<div dir="ltr"><div class="gmail_default"><font face="georgia, serif">Yes. &quot;</font>Implicitly unwrapped optional are merely optionals&quot;. That is why Xcode gives you a warning instead of an error. In other situations, such as protocol claims an implicitly unwrapped optional but you give an optional, or protocol claims a non-optional but you give an implicitly unwrapped optional, you will get errors. </div><div class="gmail_default" style="font-family:georgia,serif"><span style="font-size:13px;font-family:arial,sans-serif"><br></span></div><div class="gmail_default" style="font-family:georgia,serif"><span style="font-size:13px;font-family:arial,sans-serif">Zhaoxin</span></div></div><div class="gmail_extra"><br><div class="gmail_quote">On Sun, Jul 24, 2016 at 10:36 PM, Rod Brown <span dir="ltr">&lt;<a href="mailto:rodney.brown6@icloud.com" target="_blank">rodney.brown6@icloud.com</a>&gt;</span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div dir="auto"><div>They are not equal only because the compiler performs the unwrap for you.</div><div><br></div><div>My point is the compiler should not emit a warning that I am not adhering to the protocol. The thing I am returning is an optional - implicitly unwrapped, but optional nonetheless.</div><div><div><div dir="ltr"><div class="gmail_default"><p style="margin:0px;font-size:11px;line-height:normal;font-family:menlo;color:rgb(0,132,0)"><span><br></span></p></div></div></div></div><div>The fact they are different in actual use by MY code is irrelevant when you conform to an external protocol, as long as the actual value (an optional) is the same, which they are.</div><div><br></div><div>Implicitly unwrapped optional are merely optionals that the compiler is told to implicitly unwrap because using the ! operator where it is not required because it should never (or almost never) be nil.</div><div><br></div><div>I believe this should not throw a warning.</div><div><div class="h5"><div><br></div><div><br>On 25 Jul. 2016, at 12:16 am, Zhao Xin &lt;<a href="mailto:owenzx@gmail.com" target="_blank">owenzx@gmail.com</a>&gt; wrote:<br><br></div><blockquote type="cite"><div><div dir="ltr"><div class="gmail_default" style="font-family:georgia,serif">Implicitly unwrapped optionals and optionals are not equal.</div><div class="gmail_default" style="font-family:georgia,serif"><br></div><div class="gmail_default" style="font-family:georgia,serif"><p style="margin:0px;font-size:11px;line-height:normal;font-family:menlo;color:rgb(187,44,162)"><span>struct</span><span style="color:rgb(0,0,0)"> X {</span></p>
<p style="margin:0px;font-size:11px;line-height:normal;font-family:menlo"><span>    </span><span style="color:rgb(187,44,162)">let</span><span> value:</span><span style="color:rgb(112,61,170)">String</span><span> = </span><span style="color:rgb(209,47,27)">&quot;aa&quot;</span></p>
<p style="margin:0px;font-size:11px;line-height:normal;font-family:menlo"><span>}</span></p>
<p style="margin:0px;font-size:11px;line-height:normal;font-family:menlo;min-height:13px"><span></span><br></p>
<p style="margin:0px;font-size:11px;line-height:normal;font-family:menlo"><span style="color:rgb(187,44,162)">let</span><span> x:</span><span style="color:rgb(79,129,135)">X</span><span>? = </span><span style="color:rgb(187,44,162)">nil</span></p><p style="margin:0px;font-size:11px;line-height:normal;font-family:menlo"><span style="color:rgb(187,44,162)">let</span><span> y:</span><span style="color:rgb(79,129,135)">X</span><span>! = </span><span style="color:rgb(187,44,162)">nil</span></p><p style="margin:0px;font-size:11px;line-height:normal;font-family:menlo;color:rgb(0,132,0)"><span style="color:rgb(79,129,135)">x</span><span style="color:rgb(0,0,0)">?.</span><span style="color:rgb(79,129,135)">value</span><span style="color:rgb(0,0,0)"> </span><span>// nil</span></p><p style="margin:0px;font-size:11px;line-height:normal;font-family:menlo;color:rgb(0,132,0)"><span style="color:rgb(79,129,135)">y</span><span style="color:rgb(0,0,0)">?.</span><span style="color:rgb(79,129,135)">value</span><span style="color:rgb(0,0,0)"> </span><span>// nil</span></p><p style="margin:0px;font-size:11px;line-height:normal;font-family:menlo">



</p><p style="margin:0px;font-size:11px;line-height:normal;font-family:menlo;color:rgb(0,132,0)"><span style="color:rgb(79,129,135)">y</span><span style="color:rgb(0,0,0)">.</span><span style="color:rgb(79,129,135)">value</span><span style="color:rgb(0,0,0)"> </span><span>// fatal error: unexpectedly found nil while unwrapping an Optional value. Unless you don&#39;t use this. As this is equal to y!.value. </span></p></div><div class="gmail_default" style="font-family:georgia,serif"><br></div><div class="gmail_default" style="font-family:georgia,serif">Zhaoxin</div><div class="gmail_default" style="font-family:georgia,serif"><br></div><div class="gmail_default" style="font-family:georgia,serif"><br></div></div><div class="gmail_extra"><br><div class="gmail_quote">On Sun, Jul 24, 2016 at 9:55 PM, Rod Brown <span dir="ltr">&lt;<a href="mailto:rodney.brown6@icloud.com" target="_blank">rodney.brown6@icloud.com</a>&gt;</span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div style="word-wrap:break-word"><div>The final code to test this is:</div><div><br></div><div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo"><span style="color:#bb2ca2">import</span><span> Foundation</span></div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo"><span style="color:#bb2ca2">import</span><span> MapKit</span></div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo;min-height:13px"><span></span><br></div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo"><span style="color:#bb2ca2">class</span><span> ManifestItem: </span><span style="color:#703daa">NSObject</span><span> {</span></div><p style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo;min-height:13px"><span>    </span><br></p><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo"><span>    </span><span style="color:#bb2ca2">let</span><span> title: </span><span style="color:#703daa">String</span><span>!</span></div><span><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo"><span>    </span><span style="color:#bb2ca2">let</span><span> subtitle: </span><span style="color:#703daa">String</span><span>?</span></div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo;color:rgb(112,61,170)"><span style="color:#000000">    </span><span style="color:#bb2ca2">dynamic</span><span style="color:#000000"> </span><span style="color:#bb2ca2">var</span><span style="color:#000000"> coordinate: </span><span>CLLocationCoordinate2D</span></div><p style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo;min-height:13px"><span>    </span><br></p></span><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo"><span>    </span><span style="color:#bb2ca2">init</span><span>(title: </span><span style="color:#703daa">String</span><span>, subtitle: </span><span style="color:#703daa">String</span><span>?, coordinate: </span><span style="color:#703daa">CLLocationCoordinate2D</span><span>) {</span></div><span><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo"><span>        </span><span style="color:#bb2ca2">self</span><span>.</span><span style="color:#4f8187">title</span><span> = title</span></div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo"><span>        </span><span style="color:#bb2ca2">self</span><span>.</span><span style="color:#4f8187">subtitle</span><span> = subtitle</span></div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo"><span>        </span><span style="color:#bb2ca2">self</span><span>.</span><span style="color:#4f8187">coordinate</span><span> = coordinate</span></div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo"><span>    }</span></div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo"><span>}</span></div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo;min-height:13px"><span></span><br></div></span><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo;color:rgb(79,129,135)"><span style="color:#bb2ca2">extension</span><span style="color:#000000"> </span><span>ManifestItem</span><span style="color:#000000">: </span><span style="color:#703daa">MKAnnotation</span><span style="color:#000000"> {</span></div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo"><span>}</span></div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo;min-height:13px"><span></span><br></div></div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo;min-height:13px"><div style="font-family:Helvetica;font-size:12px">This displays a warning in beta 3, as well as in Swift 2.2 in the current Xcode.</div><div style="font-family:Helvetica;font-size:12px"><br></div><div style="font-family:Helvetica;font-size:12px">I understand why: MKAnnotation declares my title must be optional (“?”) but I am declaring it as an implicitly unwrapped optional (“!”). Still, I believe the fact is that they are both optional, and are a legitimate way of declaring “a protocol I conform to requires this to be optional, but it can be treated as implicitly inwrapped because it will never be nil”.</div><div style="font-family:Helvetica;font-size:12px"><br></div><div style="font-family:Helvetica;font-size:12px">Thoughts?</div><span><font color="#888888"><div style="font-family:Helvetica;font-size:12px"><br></div><div style="font-family:Helvetica;font-size:12px">- Rod</div></font></span></div><div><div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo;min-height:13px"><br></div><div><br></div><br><div><blockquote type="cite"><div>On 24 Jul 2016, at 11:09 PM, Zhao Xin &lt;<a href="mailto:owenzx@gmail.com" target="_blank">owenzx@gmail.com</a>&gt; wrote:</div><br><div><div dir="ltr"><div class="gmail_default" style="font-family:georgia,serif">I pasted you code into Xcode 8 beta3 playground, everything seemed fine with below code.</div><div class="gmail_default" style="font-family:georgia,serif"><br></div><div class="gmail_default" style="font-family:georgia,serif"><div style="margin:0px;font-size:11px;line-height:normal;font-family:menlo"><span style="color:rgb(187,44,162)">import</span><span> Cocoa</span></div><div style="margin:0px;font-size:11px;line-height:normal;font-family:menlo"><span style="color:rgb(187,44,162)">import</span><span> CoreLocation</span></div><div style="margin:0px;font-size:11px;line-height:normal;font-family:menlo;min-height:13px"><span></span><br></div><div style="margin:0px;font-size:11px;line-height:normal;font-family:menlo"><span style="color:rgb(187,44,162)">class</span><span> ManifestItem: </span><span style="color:rgb(112,61,170)">NSObject</span><span> {</span></div><div style="margin:0px;font-size:11px;line-height:normal;font-family:menlo"><span>    </span><span style="color:rgb(187,44,162)">let</span><span> value:    </span><span style="color:rgb(112,61,170)">String</span></div><div style="margin:0px;font-size:11px;line-height:normal;font-family:menlo"><span>    </span><span style="color:rgb(187,44,162)">let</span><span> title:    </span><span style="color:rgb(112,61,170)">String</span></div><div style="margin:0px;font-size:11px;line-height:normal;font-family:menlo"><span>    </span><span style="color:rgb(187,44,162)">let</span><span> subtitle: </span><span style="color:rgb(112,61,170)">String</span><span>?</span></div><div style="margin:0px;font-size:11px;line-height:normal;font-family:menlo;color:rgb(112,61,170)"><span>    </span><span style="color:rgb(187,44,162)">dynamic</span><span> </span><span style="color:rgb(187,44,162)">var</span><span> coordinate: </span><span>CLLocationCoordinate2D</span></div><p style="margin:0px;font-size:11px;line-height:normal;font-family:menlo;min-height:13px"><span>    </span><br></p><div style="margin:0px;font-size:11px;line-height:normal;font-family:menlo"><span>    </span><span style="color:rgb(187,44,162)">init</span><span>(value:</span><span style="color:rgb(112,61,170)">String</span><span>, title:</span><span style="color:rgb(112,61,170)">String</span><span>, subtitle:</span><span style="color:rgb(112,61,170)">String</span><span>? = </span><span style="color:rgb(187,44,162)">nil</span><span>, coordinate:</span><span style="color:rgb(112,61,170)">CLLocationCoordinate2D</span><span>) {</span></div><div style="margin:0px;font-size:11px;line-height:normal;font-family:menlo"><span>        </span><span style="color:rgb(187,44,162)">self</span><span>.</span><span style="color:rgb(79,129,135)">value</span><span> = value</span></div><div style="margin:0px;font-size:11px;line-height:normal;font-family:menlo"><span>        </span><span style="color:rgb(187,44,162)">self</span><span>.</span><span style="color:rgb(79,129,135)">title</span><span> = title</span></div><div style="margin:0px;font-size:11px;line-height:normal;font-family:menlo"><span>        </span><span style="color:rgb(187,44,162)">self</span><span>.</span><span style="color:rgb(79,129,135)">subtitle</span><span> = subtitle</span></div><div style="margin:0px;font-size:11px;line-height:normal;font-family:menlo"><span>        </span><span style="color:rgb(187,44,162)">self</span><span>.</span><span style="color:rgb(79,129,135)">coordinate</span><span> = coordinate</span></div><div style="margin:0px;font-size:11px;line-height:normal;font-family:menlo"><span>    }</span></div><div style="margin:0px;font-size:11px;line-height:normal;font-family:menlo"><span>}</span></div><div style="margin:0px;font-size:11px;line-height:normal;font-family:menlo;min-height:13px"><span></span><br></div><div style="margin:0px;font-size:11px;line-height:normal;font-family:menlo"><span style="color:rgb(187,44,162)">@objc</span><span> </span><span style="color:rgb(187,44,162)">public</span><span> </span><span style="color:rgb(187,44,162)">protocol</span><span> MKAnnotation : </span><span style="color:rgb(112,61,170)">NSObjectProtocol</span><span> {</span></div><p style="margin:0px;font-size:11px;line-height:normal;font-family:menlo;min-height:13px"><span>    </span><br></p><div style="margin:0px;font-size:11px;line-height:normal;font-family:menlo;color:rgb(0,132,0)"><span>    </span><span>// Center latitude and longitude of the annotation view.</span></div><div style="margin:0px;font-size:11px;line-height:normal;font-family:menlo;color:rgb(0,132,0)"><span>    </span><span>// The implementation of this property must be KVO compliant.</span></div><div style="margin:0px;font-size:11px;line-height:normal;font-family:menlo"><span>    </span><span style="color:rgb(187,44,162)">var</span><span> coordinate: </span><span style="color:rgb(112,61,170)">CLLocationCoordinate2D</span><span> { </span><span style="color:rgb(187,44,162)">get</span><span> }</span></div><p style="margin:0px;font-size:11px;line-height:normal;font-family:menlo;min-height:13px"><span>    </span><br></p><div style="margin:0px;font-size:11px;line-height:normal;font-family:menlo;color:rgb(0,132,0)"><span>    </span><span>// Title and subtitle for use by selection UI.</span></div><div style="margin:0px;font-size:11px;line-height:normal;font-family:menlo"><span>    </span><span style="color:rgb(187,44,162)">@objc</span><span> </span><span style="color:rgb(187,44,162)">optional</span><span> </span><span style="color:rgb(187,44,162)">var</span><span> title: </span><span style="color:rgb(112,61,170)">String</span><span>? { </span><span style="color:rgb(187,44,162)">get</span><span> }</span></div><div style="margin:0px;font-size:11px;line-height:normal;font-family:menlo"><span>    </span><span style="color:rgb(187,44,162)">@objc</span><span> </span><span style="color:rgb(187,44,162)">optional</span><span> </span><span style="color:rgb(187,44,162)">var</span><span> subtitle: </span><span style="color:rgb(112,61,170)">String</span><span>? { </span><span style="color:rgb(187,44,162)">get</span><span> }</span></div><div style="margin:0px;font-size:11px;line-height:normal;font-family:menlo"><span>}</span></div></div><div class="gmail_default" style="font-family:georgia,serif"><br></div><div class="gmail_default" style="font-family:georgia,serif">I didn&#39;t encounter the error you mentioned. But I had to remove public and add @objc as Xcode asked.</div><div class="gmail_default" style="font-family:georgia,serif"><br></div><div class="gmail_default" style="font-family:georgia,serif">Zhaoxin</div></div><div class="gmail_extra"><br><div class="gmail_quote">On Sun, Jul 24, 2016 at 12:09 PM, Rod Brown via swift-users <span dir="ltr">&lt;<a href="mailto:swift-users@swift.org" target="_blank">swift-users@swift.org</a>&gt;</span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div style="word-wrap:break-word">Hi Swift Users,<div><br><div>I just ran across an issue where I had the following code:</div><div><br></div><div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo"><span style="color:#bb2ca2">class</span><span> ManifestItem: </span><span style="color:#703daa">NSObject</span><span> {</span></div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo"><span>    </span><span style="color:#bb2ca2">let</span><span> value:    </span><span style="color:#703daa">String</span></div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo"><span>    </span><span style="color:#bb2ca2">let</span><span> title:    </span><span style="color:#703daa">String</span></div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo"><span>    </span><span style="color:#bb2ca2">let</span><span> subtitle: </span><span style="color:#703daa">String</span><span>?</span></div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo"><span><span style="color:rgb(187,44,162)">    dynamic var</span><span> coordinate: </span><span style="color:rgb(112,61,170)">CLLocationCoordinate2D</span></span></div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo"><span>}</span></div></div><div><br></div><div><br></div><div>As part of utilising this object, I needed it to conform to MKAnnotation, which declares:</div><div><br></div><div><br></div><div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo"><span style="color:#bb2ca2">public</span><span> </span><span style="color:#bb2ca2">protocol</span><span> MKAnnotation : </span><span style="color:#703daa">NSObjectProtocol</span><span> {</span></div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo;min-height:13px"><span>    </span><br></div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo;color:rgb(0,132,0)"><span>    </span><span>// Center latitude and longitude of the annotation view.</span></div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo;color:rgb(0,132,0)"><span>    </span><span>// The implementation of this property must be KVO compliant.</span></div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo"><span>    </span><span style="color:#bb2ca2">public</span><span> </span><span style="color:#bb2ca2">var</span><span> coordinate: </span><span style="color:#703daa">CLLocationCoordinate2D</span><span> { </span><span style="color:#bb2ca2">get</span><span> }</span></div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo;min-height:13px"><span>    </span><br></div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo;color:rgb(0,132,0)"><span>    </span><span>// Title and subtitle for use by selection UI.</span></div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo"><span>    </span><span style="color:#bb2ca2">optional</span><span> </span><span style="color:#bb2ca2">public</span><span> </span><span style="color:#bb2ca2">var</span><span> title: </span><span style="color:#703daa">String</span><span>? { </span><span style="color:#bb2ca2">get</span><span> }</span></div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo"><span>    </span><span style="color:#bb2ca2">optional</span><span> </span><span style="color:#bb2ca2">public</span><span> </span><span style="color:#bb2ca2">var</span><span> subtitle: </span><span style="color:#703daa">String</span><span>? { </span><span style="color:#bb2ca2">get</span><span> }</span></div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo"><span>}</span></div></div><div><span><br></span></div><div><span>I added the appropriate extension to “ManifestItem” and promptly got reminded with an error that MKAnnotation requires title be an optional.</span></div><div><span><br></span></div><div><span>To do this, I converted “title” in my ManifestItem class to be an implicitly unwrapped optional. This value can never be nil, and should be treated as such throughout my code. However, the compiler still emits a warning. &quot;</span>Type of &#39;title&#39; has different optionality than expected by protocol ‘MKAnnotation&#39; &quot;</div><div><span><br></span></div><div><span>I’m wondering if this should be considered a bug? I am actually comforming correctly to the protocol - title is optional, though implicitly unwrapped. Should I have to make “title” optional, and every use of it optional or a force unwrap, when my code verifies that the manifest item is legitimately invalid if title property is nil?</span></div><div><span><br></span></div><div><span>I think this is valid use of the implicitly unwrapped optional. It allows you to say “this is optional for some reason, but should generally always be assumed to be non null.” This sounds remarkably similar to my use case &quot;</span>this is optional for conformance reasons, but should generally always be assumed to be non null.”</div><div><br></div><div>Thanks for your opinions,</div><div><br></div><div>Rod</div></div></div><br>_______________________________________________<br>
swift-users mailing list<br>
<a href="mailto:swift-users@swift.org" target="_blank">swift-users@swift.org</a><br>
<a href="https://lists.swift.org/mailman/listinfo/swift-users" rel="noreferrer" target="_blank">https://lists.swift.org/mailman/listinfo/swift-users</a><br>
<br></blockquote></div><br></div>
</div></blockquote></div><br></div></div></div></blockquote></div><br></div>
</div></blockquote></div></div></div></blockquote></div><br></div>