<div dir="ltr"><div class="gmail_default" style="font-family:georgia,serif">You can write.</div><div class="gmail_default" style="font-family:georgia,serif"><br></div><div class="gmail_default"><div align="left" style="font-family:arial,sans-serif;font-size:13px"><font color="#AA0D91" face="Times New Roman"><span style="font-size:11pt">func</span></font> <font face="Times
New Roman"><span style="font-size:11pt">bar() {</span></font></div><div align="left" style="font-family:arial,sans-serif;font-size:13px"><font face="Times New Roman"><span style="font-size:11pt"> </span></font> <font color="#AA0D91" face="Times New Roman"><span style="font-size:11pt">if</span></font> <font color="#3F6E74" face="Times New Roman"><span style="font-size:11pt">xxx</span></font> <font face="Times
New Roman"><span style="font-size:11pt">{</span></font></div><div align="left" style="font-family:arial,sans-serif;font-size:13px"><font face="Times New Roman"><span style="font-size:11pt"> </span></font> <font color="#AA0D91" face="Times New Roman"><span style="font-size:11pt">switch</span></font> <font color="#3F6E74" face="Times New Roman"><span style="font-size:11pt">yyy</span></font> <font face="Times
New Roman"><span style="font-size:11pt">{</span></font></div><div align="left" style="font-family:arial,sans-serif;font-size:13px"><font face="Times New Roman"><span style="font-size:11pt"> </span></font> <font color="#AA0D91" face="Times New Roman"><span style="font-size:11pt">case</span></font> <font face="Times
New Roman"><span style="font-size:11pt">.c1:</span></font></div><div align="left" style="font-family:arial,sans-serif;font-size:13px"><font face="Times New Roman"><span style="font-size:11pt"> </span></font> <font color="#26474B" face="Times New Roman"><span style="font-size:11pt">doSomething1</span></font><font face="Times New Roman"><span style="font-size:11pt">()</span></font></div><div align="left" style="font-family:arial,sans-serif;font-size:13px"><font face="Times New Roman"><span style="font-size:11pt"> </span></font> <font color="#AA0D91" face="Times New Roman"><span style="font-size:11pt">case</span></font> <font face="Times
New Roman"><span style="font-size:11pt">.c2:</span></font></div><div align="left"><font face="Times\a\a New Roman"><span style="font-size:15px"><b> guard let foo= someOptional else { fatalError("someOptional should never be nil here!") }</b></span></font></div><div align="left" style="font-family:arial,sans-serif;font-size:13px"><b><font face="Times New Roman"><span style="font-size:11pt"> </span></font> <font color="#26474B" face="Times New Roman"><span style="font-size:11pt">doSomethingWithValue</span></font><font face="Times New Roman"><span style="font-size:11pt">(<font color="#3f6e74">foo</font></span></font><font face="Times New Roman"><span style="font-size:11pt">)</span></font><font color="#007400"><font face="Times New Roman"> </font></font></b></div><div align="left" style="font-family:arial,sans-serif;font-size:13px"><font face="Times New Roman"><span style="font-size:11pt"> }</span></font></div><div align="left" style="font-family:arial,sans-serif;font-size:13px"><font face="Times New Roman"><span style="font-size:11pt"> }</span></font> <font color="#AA0D91" face="Times New Roman"><span style="font-size:11pt">else</span></font> <font face="Times
New Roman"><span style="font-size:11pt">{</span></font></div><div align="left" style="font-family:arial,sans-serif;font-size:13px"><font face="Times New Roman"><span style="font-size:11pt"> </span></font> <font color="#26474B" face="Times New Roman"><span style="font-size:11pt">doSomething2</span></font><font face="Times New Roman"><span style="font-size:11pt">()</span></font></div><div align="left" style="font-family:arial,sans-serif;font-size:13px"><font face="Times New Roman"><span style="font-size:11pt"> }</span></font></div><div align="left" style="font-family:arial,sans-serif;font-size:13px"><font face="Times New Roman"><span style="font-size:11pt"> }</span></font></div><div align="left" style="font-family:arial,sans-serif;font-size:13px"><font face="Times New Roman"><span style="font-size:11pt"><br></span></font></div><div align="left" style="font-family:arial,sans-serif;font-size:13px"><font face="Times New Roman"><span style="font-size:11pt">zhaoxin</span></font></div></div></div><div class="gmail_extra"><br><div class="gmail_quote">On Mon, Jan 25, 2016 at 6:34 PM, CosynPa 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">
<div bgcolor="#FFFFFF" text="#000000">
<span>
<div align="left"><span><font face="Times New Roman"><span style="font-size:11pt">For example, I have a function that
set some optional value:<br>
<br>
</span></font> <font color="#AA0D91" face="Times New
Roman"><span style="font-size:11pt">func</span></font> <font face="Times New Roman"><span style="font-size:11pt">foo() {</span></font></span></div>
<div align="left"><font face="Times New Roman"><span style="font-size:11pt"> </span></font> <font color="#AA0D91" face="Times New Roman"><span style="font-size:11pt">if</span></font> <font color="#3F6E74" face="Times New Roman"><span style="font-size:11pt">xxx</span></font> <font face="Times
New Roman"><span style="font-size:11pt">{</span></font></div>
<div align="left"><font face="Times New Roman"><span style="font-size:11pt"> </span></font> <font color="#AA0D91" face="Times New Roman"><span style="font-size:11pt">switch</span></font> <font color="#3F6E74" face="Times New Roman"><span style="font-size:11pt">yyy</span></font> <font face="Times
New Roman"><span style="font-size:11pt">{</span></font></div>
<div align="left"><font face="Times New Roman"><span style="font-size:11pt"> </span></font> <font color="#AA0D91" face="Times New Roman"><span style="font-size:11pt">case</span></font> <font face="Times
New Roman"><span style="font-size:11pt">.c1:</span></font></div>
<div align="left"><font face="Times New Roman"><span style="font-size:11pt"> </span></font> <font color="#3F6E74" face="Times New Roman"><span style="font-size:11pt">someOptional</span></font> <font face="Times New Roman"><span style="font-size:11pt">=</span></font>
<font color="#AA0D91" face="Times New Roman"><span style="font-size:11pt">nil</span></font></div>
<div align="left"><font face="Times New Roman"><span style="font-size:11pt"> </span></font> <font color="#AA0D91" face="Times New Roman"><span style="font-size:11pt">case</span></font> <font face="Times
New Roman"><span style="font-size:11pt">.c2:</span></font></div>
<div align="left"><font face="Times New Roman"><span style="font-size:11pt"> </span></font> <font color="#3F6E74" face="Times New Roman"><span style="font-size:11pt">someOptional</span></font> <font face="Times New Roman"><span style="font-size:11pt">=</span></font>
<font color="#1C00CF" face="Times New Roman"><span style="font-size:11pt">5</span></font></div>
<div align="left"><font face="Times New Roman"><span style="font-size:11pt"> }</span></font></div>
<div align="left"><font face="Times New Roman"><span style="font-size:11pt"> }</span></font> <font color="#AA0D91" face="Times New Roman"><span style="font-size:11pt">else</span></font> <font face="Times
New Roman"><span style="font-size:11pt">{</span></font></div>
<div align="left"><font face="Times New Roman"><span style="font-size:11pt"> </span></font> <font color="#3F6E74" face="Times New Roman"><span style="font-size:11pt">someOptional</span></font> <font face="Times New Roman"><span style="font-size:11pt">=</span></font>
<font color="#AA0D91" face="Times New Roman"><span style="font-size:11pt">nil</span></font></div>
<div align="left"><font face="Times New Roman"><span style="font-size:11pt"> }</span></font></div>
<div align="left"><font face="Times New Roman"><span style="font-size:11pt"> }</span></font></div>
<div align="left"><font face="Times New Roman"><span style="font-size:11pt"><br>
Later I want to do something with the optional value. And I
know the optional is always not nil when xxx is satisfied
and yyy is in c2 case, so I just use force unwrapping. <br>
<br>
</span></font></div>
<div align="left"><font face="Times New Roman"><span style="font-size:11pt"> </span></font> <font color="#AA0D91" face="Times New Roman"><span style="font-size:11pt">func</span></font> <font face="Times
New Roman"><span style="font-size:11pt">bar() {</span></font></div>
<div align="left"><font face="Times New Roman"><span style="font-size:11pt"> </span></font> <font color="#AA0D91" face="Times New Roman"><span style="font-size:11pt">if</span></font> <font color="#3F6E74" face="Times New Roman"><span style="font-size:11pt">xxx</span></font> <font face="Times
New Roman"><span style="font-size:11pt">{</span></font></div>
<div align="left"><font face="Times New Roman"><span style="font-size:11pt"> </span></font> <font color="#AA0D91" face="Times New Roman"><span style="font-size:11pt">switch</span></font> <font color="#3F6E74" face="Times New Roman"><span style="font-size:11pt">yyy</span></font> <font face="Times
New Roman"><span style="font-size:11pt">{</span></font></div>
<div align="left"><font face="Times New Roman"><span style="font-size:11pt"> </span></font> <font color="#AA0D91" face="Times New Roman"><span style="font-size:11pt">case</span></font> <font face="Times
New Roman"><span style="font-size:11pt">.c1:</span></font></div>
<div align="left"><font face="Times New Roman"><span style="font-size:11pt"> </span></font> <font color="#26474B" face="Times New Roman"><span style="font-size:11pt">doSomething1</span></font><font face="Times New Roman"><span style="font-size:11pt">()</span></font></div>
<div align="left"><font face="Times New Roman"><span style="font-size:11pt"> </span></font> <font color="#AA0D91" face="Times New Roman"><span style="font-size:11pt">case</span></font> <font face="Times
New Roman"><span style="font-size:11pt">.c2:</span></font></div>
<div align="left"><font face="Times New Roman"><span style="font-size:11pt"> </span></font> <font color="#26474B" face="Times New Roman"><span style="font-size:11pt">doSomethingWithValue</span></font><font face="Times New Roman"><span style="font-size:11pt">(</span></font><font color="#3F6E74" face="Times New Roman"><span style="font-size:11pt">someOptional</span></font> <font face="Times New Roman"><span style="font-size:11pt">!)</span></font><font color="#007400"><font face="Times New Roman"> </font></font><span><span><font face="Times New Roman"><span style="font-size:11pt"><span style="color:rgb(0,116,0)"></span></span></font><font color="#007400" face="Times New Roman"><span style="font-size:11pt">// force unwrapping, not very
good</span></font></span></span>
</div>
<div align="left"><font face="Times New Roman"><span style="font-size:11pt"> }</span></font></div>
<div align="left"><font face="Times New Roman"><span style="font-size:11pt"> }</span></font> <font color="#AA0D91" face="Times New Roman"><span style="font-size:11pt">else</span></font> <font face="Times
New Roman"><span style="font-size:11pt">{</span></font></div>
<div align="left"><font face="Times New Roman"><span style="font-size:11pt"> </span></font> <font color="#26474B" face="Times New Roman"><span style="font-size:11pt">doSomething2</span></font><font face="Times New Roman"><span style="font-size:11pt">()</span></font></div>
<div align="left"><font face="Times New Roman"><span style="font-size:11pt"> }</span></font></div>
<div align="left"><font face="Times New Roman"><span style="font-size:11pt"> }<br>
<br>
But this is not very good, since you can't tell from the
code why the optional is not nil, and if the function foo is
changed, you are not aware of the fact that the force
unwrapping is no longer valid. So is there some better
solution?<br>
<br>
</span></font></div>
</span>
</div>
<br>_______________________________________________<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/mailman/listinfo/swift-users</a><br>
<br></blockquote></div><br></div>