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
Post a Comment