Reputation: 41480
How do you do optional binding on an optional of optional?
For example, assume UIViewController's navigationController property is an optional of optional. Should I use method #1 or method #2, or is there a third way?
Method #1
if let navigationController = viewController.navigationController! {
}
or
Method #2
if let optionalNavigationController = viewController.navigationController {
let navigationController = optionalNavigationController {
}
}
Upvotes: 1
Views: 87
Reputation: 285082
If you know that the view controller has a navigation controller, because you designed it in Interface Builder or created it programmatically, declare a variable
var navigationController : UINavigationController!
and assign the navigation controller in viewDidLoad()
as implicit unwrapped optional
navigationController = viewController!.navigationController!
If you don't know, use a normal optional binding as described in the other answers
Upvotes: 0
Reputation: 4176
Much more functional-programming solution:
if let a = optionalOfOptional?.map({$0}) {
println(a)
}
Upvotes: 0
Reputation: 12015
There is another solution:
if let stillOptional = viewController.navigationController, let notOptional = stillOptional {
//use notOptional here
}
Upvotes: 1