When to use @objc in Swift?

Wingzero picture Wingzero · Jun 12, 2015 · Viewed 50.3k times · Source

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?

Answer

Reming Hsu picture Reming Hsu · Jun 12, 2015

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