I'm in the process of introducing Swift to an Objective-C app.
I have a UIViewController subclass where I want to hide a button if a certain property is nil. My Swift code looks like this, using optional binding:
if let optionalVar = modelObject.propertyThatCouldBeNil {
button.setTitle(...)
} else {
button.hidden = true
}
My hope was that when propertyThatCouldBeNil was nil, the if wasn't satisfied and control would continue to the else. But that's not what's happening. In fact if I set a breakpoint, what I see is...
(lldb) po optionalVar
nil
(lldb) po modelObject.propertyThatCouldBeNil
▿ Optional<Optional<String>>
- some : nil
That latter one is what's tripping me up a bit. I think it should be Optional, not nested, correct?
Some more info...
Since I'm using modelObject throughout this class, for convenience it's defined as an implicitly unwrapped optional...
var modelObject : ModelObjectClass!
ModelObjectClass is actually an Objective-C class, and the property is read only and declared as such...
@property (readonly, nullable) NSString *propertyThatCouldBeNil;
In it's implementation, it actually acts as a sort of proxy for another ready only property...
- (NSString*) propertyThatCouldBeNil {
return self.someOtherProperty;
}
And the someOtherProperty is nullable as well...
@property (nullable, nonatomic, retain) NSString *someOtherProperty;
Any insight as to why the optional binding isn't working as I expect?