Tuesday, July 31, 2018

Objective-C Features That I Wish Existed

Daniel Lazarenko (via Gianluca Bertani):

The features listed here are chosen to have a relatively small scope in order to be implemented without major changes and in a backwards compatible way in the spirit of Objective-C. Thus they are not meant to turn Objective-C into a modern experimental language like Swift, but should make programming experience better and reduce boilerplate.

[…]

In 2015 Xcode 6.3 introduced nullability annotations to Objective-C. With those you can express an intent that a pointer to an object can never be nil. Unfortunately the only observable check that the Objective-C compiler does with that is that it prevents you from passing the nil constant in places where a nonnull type is expected. If you pass in a nullable pointer variable (not a nil constant), this is silently allowed[…]

[…]

Imagine a piece of code that uses a lot of blocks, including nested blocks where on completion you want to do something else asynchronously, so you have to make a second strongSelf. This pattern becomes boilerplate code! It is possible to shrink the lines by using @weakify/@strongify custom helper macros, but you still have to write them and have them available in your project.

Update (2018-08-02): Heath Borders:

If you use -Wnullable-to-nonnull-conversion and nullability annotations on local variables and properties, you get proper warnings when you pass a _Nullable to a _Nonnull parameter or assign a _Nullable rvalue to a _Nonnull lvalue.

And if you ever need to cast a _Nullable to a _Nonnull, this is the safe way to do so.

3 Comments RSS · Twitter


the static analyzer gives additional nullability warnings beyond what the compiler gives, IIRC.


@bob Yes, that has been my experience as well.


Not a bad list. I don't know if everything is needed (I'm not a huge fan of generics), but a decent list nonetheless.

Leave a Comment