× {{alert.msg}} Never ask again
Get notified about new tutorials RECEIVE NEW TUTORIALS

Xcode 6 Beta7 NSDictionary to Swift

Antonio Bello
Mar 01, 2015
<p>The problem is that <code>UIScrollView.superview</code> is an optional, so you have to put the unwrapped value in the dictionary</p> <pre><code>let views:NSDictionary = [ "leftView": _leftVC.view, "rightView": _rightVC.view, "outerView": _scrollView.superview! ]; </code></pre> <p>Use a safer logic instead of an implicitly unwrapped (i.e. check that <code>superview</code> is not nil), unless you are 100% sure it contains a non nil value.</p> <p>Even if the <code>views</code> variable is of <code>NSDictionary</code> type, the dictionary literal you are using to initialize it evaluates to a swift dictionary - it is then silently bridged to a <code>NSDictionary</code>.</p> <p>The reason why the compiler complains is that being <code>_scrollView.superview</code> an optional, it can potentially be nil, and that's not allowed.</p> <p>As noted by @JackLawrance, a dictionary can have non uniform value types even when initialized with literals.</p> <p>Sidenote: when will we get more meaningful error messages? :)</p> <p>This tip was originally posted on <a href="http://stackoverflow.com/questions/25649875/Xcode%206%20Beta7%20NSDictionary%20to%20Swift/25650106">Stack Overflow</a>.</p>
comments powered by Disqus