So I\'ve been developing an app in Swift, and today I spent nearly an hour debugging a problem that turned out to be completely unexpected. It all resulted from the code bel
my advice is to use this nice coalescing ??
if textfieldDate.text?.isEmpty ?? true {
// the text is either nil or empty but its all we want to know
}
If the bool is part if Core Data (aka NSNumber), you should do it like this.
if (isHero.isAI?.boolValue != nil)
Regards
This is a known issue that is being tracked on the SwiftInFlux repo, which includes this quote from Chris Lattner on the Apple developer forums.
This problem exists with any optional of something that conforms to the LogicValue protocol (e.g. nested optionals, optional of bool, etc). We consider it serious issue that needs to be fixed for 1.0 and have some ideas, but haven't settled on a solution yet.
So, this issue doesn't just effect optional Bools, but any optional type that conforms to the LogicValue protocol (defined as).
protocol LogicValue {
func getLogicValue() -> Bool
}
Anyway as far as recommendations on how to work around this go, it's hard to recommend any one specific solution considering that Apple hasn't given an indication of how they intend to solve this in the future, but I would imagine that continuing to explicitly check the value of the Bool would be the way to go.
if (hero.isAI == true) {
// stuff
}
In fact, after some further reading, the quote listed above continues to read:
For this common case, the simplest answer would be to produce a warning for "if x" and require someone to explictly write "if x != nil" or "if x == true" to make it explicit what they want.