ios - How to use Swift closure in OC -


in swift uiviewcontroller, code:

import foundation import uikit  typealias sayhello = ()->string  @objc class swiftviewcontroller:uiviewcontroller{  var sayhello:sayhello?  override func viewdidload() {      super.viewdidload()      if let helloclosure = sayhello{         helloclosure()     } }} 

in oc uiviewcontroller, code:

swiftviewcontroller.sayhello = ^(){      return "hello block"; }; 

when xcode compile oc file ,it shows error:

incompatible block pointer types assigning 'nsstring * _nonnull(^_nullable)(void)'from 'char *(^)(void)'

where mistake ... (sorry, forgot @ before "hello block" in oc file. it's careless mistake. works fine. thank you!)

you forgot @ before "hello block", returning c-string, , because didn't explicitly specified return type block, compiler infers char*, incompatible nsstring*.

if declare block ^nsstring*(){, you'll error telling return value doesn't match, , suggest add @


Comments

Popular posts from this blog

android - Why am I getting the message 'Youractivity.java is not an activity subclass or alias' -

python - How do I create a list index that loops through integers in another list -

c# - “System.Security.Cryptography.CryptographicException: Keyset does not exist” when reading private key from remote machine -