I am having a model class as follows, which is in a library written in Objective-C. I am consuming this class in my swift project. In swift it becomes property of type String!
. Sometimes that property will be nil. So, I am testing the nil validation as follows:
Vendor.h
@interface Vendor: NSObject {
@property (nonatomic, strong) NSString *firstName;
@property (nonatomic, strong) NSString *lastName;
@property (nonatomic, strong) NSString *middleName;
}
In my Swift project, I am checking the nil validation for the middleName property as below:
if anObject.middleNam != nil { // Here, It throws runtime error: fatal error: Unexpectedly found nil while unwrapping an Optional value
}
It throws me an following runtime error:
fatal error: unexpectedly found nil while unwrapping an Optional value
If the objective-C properties exposed in swift as String?
then I would have used the following:
if let middleName = anObject.middleName {
}
How would I check for the unwrapped optional variable.
Thanks in advance.
if let
with implicitly unwrapped optionals – although the!= nil
check should work properly withmiddleName
, how isanObject
defined? – Genip