[swift-users] protocols, optional, and public

Rick Mann rmann at latencyzero.com
Fri Aug 26 19:52:52 CDT 2016

I'm wrapping CLLocationManager (and its delegate), and I'm trying to create a protocol like this. This is directly cribbed from CLLocationManagerDelegate.

LZLocationManagerDelegate : NSObjectProtocol
    optional public func locationManager(manager: LZLocationManager, didUpdateLocations locations: [CLLocation])

But I get these errors:

LZLocationManager.swift:16:5: 'optional' can only be applied to members of an @objc protocol
LZLocationManager.swift:70:41: Cannot use optional chaining on non-optional value of type '(LZLocationManager, didUpdateLocations: [CLLocation]) -> ()'

Apple's version compiles flawlessly (comments removed):

public protocol CLLocationManagerDelegate : NSObjectProtocol {
    @available(iOS 6.0, *)
    optional public func locationManager(manager: CLLocationManager, didUpdateLocations locations: [CLLocation])

    // ...remainder removed for brevity

Adding @objc fixes it, but I would have thought NSObjectProtocol implied that. Also, when I look at the Apple Swift exports, it's not there (is that a limitation of the exported view?).

Rick Mann
rmann at latencyzero.com

More information about the swift-users mailing list