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

SVG stroke-linecap doesn't work for circles in Firefox? -

routes - Laravel 4 Wildcard Routing to Different Controllers -

cross browser - XSLT namespace-alias Not Working in Firefox or Chrome -