iosobjective-cswiftselectorprivate

When to use @objc in Swift?


In Swift, I see some methods like:

@objc private func doubleTapGestureRecognized(recognizer: UITapGestureRecognizer)

I was wondering, when to use @objc? I read some documents, but they are saying when you want it to be callable in Objective-C, you should add @objc flag

However, this is a private function in Swift, what does the @obj do?


Solution

  • private mean it visible only in Swift. so use @objc to visible in Objective-C. If you have a func to selector a private func in swift, it is required.

    The @objc attribute makes your Swift API available in Objective-C and the Objective-C runtime.

    See: https://developer.apple.com/library/ios/documentation/Swift/Conceptual/BuildingCocoaApps/MixandMatch.html

    https://developer.apple.com/library/ios/documentation/Swift/Conceptual/BuildingCocoaApps/InteractingWithObjective-CAPIs.html