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

How to check for self with contains() in Swift?

Antonio Bello
Apr 30, 2015
<p>If you look at the declaration of the <code>viewControllers</code> property, you notice that it's <code>[AnyObject]!</code> and not <code>[UIViewController]!</code>.</p> <p>The <code>contains</code> function requires that the sequence element implements the <code>Equatable</code> protocol, which <code>AnyObject</code> doesn't.</p> <p>The solution is to make an explicit downcast of that array, using optional binding:</p> <pre><code>if let viewControllers = self.navigationController?.viewControllers as? [UIViewController] { if (contains(viewControllers, self)) { println("Back button not pressed") } else { self.updateSearchQueryModel() } } </code></pre> <p>This tip was originally posted on <a href="http://stackoverflow.com/questions/29954229/How%20to%20check%20for%20self%20with%20contains()%20in%20Swift?/29954498">Stack Overflow</a>.</p>
comments powered by Disqus