<div dir="ltr"><div class="gmail_default"><blockquote class="gmail_quote" style="font-family:georgia,serif;margin:0px 0px 0px 0.8ex;border-left-width:1px;border-left-color:rgb(204,204,204);border-left-style:solid;padding-left:1ex">“Optional requirements are available so that you can write code that interoperates with Objective-C. Both the protocol and the optional requirement must be marked with the @objc attribute. ”<br></blockquote><blockquote class="gmail_quote" style="font-family:georgia,serif;margin:0px 0px 0px 0.8ex;border-left-width:1px;border-left-color:rgb(204,204,204);border-left-style:solid;padding-left:1ex"> </blockquote><blockquote class="gmail_quote" style="font-family:georgia,serif;margin:0px 0px 0px 0.8ex;border-left-width:1px;border-left-color:rgb(204,204,204);border-left-style:solid;padding-left:1ex">from: Apple Inc. “The Swift Programming Language (Swift 3 Beta)”。 iBooks. <a href="https://itun.es/us/k5SW7.l">https://itun.es/us/k5SW7.l</a></blockquote><div style="font-family:georgia,serif"><br></div><div style="font-family:georgia,serif">I think the CLLocationManagerDelegate you saw was converted from Objective-C, which means it was not written in Swift directly. It was just the counterpart called interface. In swift, interface is automatically generated. It is not the source file.</div><div style="font-family:georgia,serif"><br></div><div style="font-family:georgia,serif">For example, you can create a new project, open a swift file, open assistant window (option+command+enter). Add a protocol in your swift file.</div><div style="font-family:georgia,serif"><br></div><div><blockquote class="gmail_quote" style="font-family:georgia,serif;margin:0px 0px 0px 0.8ex;border-left-width:1px;border-left-color:rgb(204,204,204);border-left-style:solid;padding-left:1ex">public protocol UnmanagedCopy {<br> func unmanagedCopy() -> Self<br>}</blockquote><div style="font-family:georgia,serif"><br></div><div style="font-family:georgia,serif">Build your project. Reload your interface file in the assistant window by changing to other files and back. You can see the counterpart is </div><div style="font-family:georgia,serif"><br></div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left-width:1px;border-left-color:rgb(204,204,204);border-left-style:solid;padding-left:1ex"><font face="georgia, serif">public protocol UnmanagedCopy {</font><font face="georgia, serif"><br></font><font face="georgia, serif"> </font> </blockquote><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left-width:1px;border-left-color:rgb(204,204,204);border-left-style:solid;padding-left:1ex"><font face="georgia, serif"> public func unmanagedCopy() -> Self<br></font><font face="georgia, serif">} </font></blockquote><div> </div><div>You should aware that though there is a `public func` in interface. You can not do it in protocol definition in source file.</div></div><div><br></div><div>Further more, I also test a new protocol Foo.</div><div><br></div><div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left-width:1px;border-left-color:rgb(204,204,204);border-left-style:solid;padding-left:1ex">@objc public protocol Foo {<br> @objc optional func bar()<br>}</blockquote><div><br></div><div>It turns out the counterpart is not as my expectation. The @objc remains.</div><div><br></div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left-width:1px;border-left-color:rgb(204,204,204);border-left-style:solid;padding-left:1ex"> @objc public protocol Foo {<br> @objc optional public func bar()<br>}</blockquote></div><div style="font-family:georgia,serif"><br></div><div style="font-family:georgia,serif">Since my testing code was written in Swift, I thought the reason of that was because the interface generated from Objective-C code was not identical as from Swift. For an interface file generated from Objective-C, the source code was definitely working in Objective-C. So the interface file was for Swift only, which means there was no need to apply those restrictions.</div><div style="font-family:georgia,serif"><br></div><div style="font-family:georgia,serif">Zhaoxin</div></div></div><div class="gmail_extra"><br><div class="gmail_quote">On Sat, Aug 27, 2016 at 8:52 AM, Rick Mann via swift-users <span dir="ltr"><<a href="mailto:swift-users@swift.org" target="_blank">swift-users@swift.org</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">I'm wrapping CLLocationManager (and its delegate), and I'm trying to create a protocol like this. This is directly cribbed from CLLocationManagerDelegate.<br>
<br>
---------------<br>
public<br>
protocol<br>
LZLocationManagerDelegate : NSObjectProtocol<br>
{<br>
optional public func locationManager(manager: LZLocationManager, didUpdateLocations locations: [CLLocation])<br>
}<br>
---------------<br>
<br>
But I get these errors:<br>
<br>
LZLocationManager.swift:16:5: 'optional' can only be applied to members of an @objc protocol<br>
LZLocationManager.swift:70:41: Cannot use optional chaining on non-optional value of type '(LZLocationManager, didUpdateLocations: [CLLocation]) -> ()'<br>
<br>
Apple's version compiles flawlessly (comments removed):<br>
<br>
---------------<br>
public protocol CLLocationManagerDelegate : NSObjectProtocol {<br>
@available(iOS 6.0, *)<br>
optional public func locationManager(manager: CLLocationManager, didUpdateLocations locations: [CLLocation])<br>
<br>
// ...remainder removed for brevity<br>
}<br>
---------------<br>
<br>
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?).<br>
<span class="HOEnZb"><font color="#888888"><br>
--<br>
Rick Mann<br>
<a href="mailto:rmann@latencyzero.com">rmann@latencyzero.com</a><br>
<br>
<br>
______________________________<wbr>_________________<br>
swift-users mailing list<br>
<a href="mailto:swift-users@swift.org">swift-users@swift.org</a><br>
<a href="https://lists.swift.org/mailman/listinfo/swift-users" rel="noreferrer" target="_blank">https://lists.swift.org/<wbr>mailman/listinfo/swift-users</a><br>
</font></span></blockquote></div><br></div>