Can someone elaborate why is nonnull
introduced in iOS 9 ?
For example, the NSArray
method + (instancetype)array;
is now + (instancetype nonnull)array;
Refer to : https://developer.apple.com/library/prerelease/ios/releasenotes/General/iOS90APIDiffs/frameworks/Foundation.html
Is this an objective-c level feature, and how would this affect the existing apps ?
That's where the @objc attribute comes in: when you apply it to a class or method it instructs Swift to make those things available to Objective-C as well as Swift code.
id is the generic object pointer, an Objective-C type representing "any object". An instance of any Objective-C class can be stored in an id variable.
So a good test might be: if (title == (id)[NSNull null] || title. length == 0 ) title = @"Something"; Show activity on this post.
If you want to make the method optional, you must specify the objc keyword as you have done, regardless if you are interoperating with Objective-C or not. At that point, everything else should work in your example.
They have made sure that wherever the type is not-nullable it is now a nonnull
type.
Like earlier NSMutableArray addObject
method was
- (void)addObject:(ObjectType)anObject
and now it has been changed to
- (void)addObject:(ObjectType nonnull)anObject
So it means you cannot pass a null object (nil
) to this method. Same way, in your case
+ (instancetype nonnull) array
method will never return nil
.
Reference: https://developer.apple.com/swift/blog/?id=25
nonnull
is a keyword to tell the compiler that the return value (or parameter, or property) will never be nil. This was introduced in a previous version of Xcode to enable better inter operability between Obj-C and Swift's optional types.
You can learn more about it on the official Swift blog
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With