Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is nonnull in objective C?

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 ?

like image 419
Mehul Parmar Avatar asked Jun 23 '15 11:06

Mehul Parmar


People also ask

What is the use of @objc?

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.

What is ID type in Objective-C?

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.

How do I check if a string is null in Objective-C?

So a good test might be: if (title == (id)[NSNull null] || title. length == 0 ) title = @"Something"; Show activity on this post.

How do you declare optional in Objective-C?

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.


2 Answers

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

like image 96
Mrunal Avatar answered Sep 30 '22 21:09

Mrunal


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

like image 25
rounak Avatar answered Sep 30 '22 20:09

rounak