nanana
nanana

Reputation: 77

Invoke Swift closure in Objective-C

I want to invoke a Swift closure in Objective-C.

There is an error like this even though I declared the function:

No visible @interface for “User” declares the selector “isReady”

Swift:

@objcMember
class User:NSObject {
    func isReady(isTrue: Bool) -> Bool {
        return true
    }
}

Objective-C:

User *user = [[User alloc] init];
[_user isReady]. <-  error

Upvotes: 0

Views: 125

Answers (1)

Andrew Romanov
Andrew Romanov

Reputation: 5066

Add to the function @objc modifier:

@objcMember
class User:NSObject {
 @objc public func isReady(isTrue: Bool) -> Bool {
  return true
   }
 }

And add public modifier to the function to allow access from other modules (swift code builds as module and ObjC code should export it and access via open interfaces).

Upvotes: 1

Related Questions