Questions tagged [objective-c-nullability]

This tag should be used for questions about the nullability annotations added to Objective-C with Xcode 6.3.

Xcode 6.3 added nullability annotations to Objective-C to help improve the interoperability between Objective-C and Swift.

Specifically, these annotations allow Objective-C whether Swift should treat objects in methods defined in Objective-C as optionals or non-optionals.

The posisble annotations are:

  • nullable
  • nonnull

But there also exists the NS_ASSUME_NONNULL_BEGIN and NS_ASSUME_NONNULL_END macros for marking entire sections of code as nonnull.

33 questions
175
votes
4 answers

Difference between nullable, __nullable and _Nullable in Objective-C

With Xcode 6.3 there were new annotations introduced for better expressing the intention of API's in Objective-C (and to ensure better Swift support of course). Those annotations were of course nonnull, nullable and null_unspecified. But with Xcode…
Legoless
  • 10,942
  • 7
  • 48
  • 68
77
votes
6 answers

Pointer is missing a nullability type specifier

In Xcode 7 GM I started to get this warning: Pointer is missing a nullability type specifier (_Nonnull, _Nullable, or _Null_unspecified) In the following function declaration (NSUserDefaults extension) - (void)setObject:(nullable id)value …
kelin
  • 11,323
  • 6
  • 67
  • 104
25
votes
2 answers

Objective-C Nullability: Qualifying Constant Strings

I have gotten into a pretty good habit of declaring and using constant strings for things like NSNotification names. I declare them like so: extern NSString * const ABCAwesomeThingHappenedNotification; With the introduction of Xcode 6.3 and Swift…
edelaney05
  • 6,822
  • 6
  • 41
  • 65
19
votes
3 answers

How to Make Compiler Assume `nullable` by Default

Since Xcode 6.3, types in Objective-C can be marked with nullable or nonnull, here is Apple's blog post about this. Problem is, when neither is specified, then the compiler imports the Objective-C code as implicitly unwrapped into Swift, e.g.…
fabb
  • 11,660
  • 13
  • 67
  • 111
13
votes
3 answers

How do I safely cast a `_Nullable` to a `_Nonnull` in Objective-C?

When compiling with -Wnullable-to-nonnull-conversion, we get a proper warning with the following code: NSString * _Nullable maybeFoo = @"foo"; ^(NSString * _Nonnull bar) { }(maybeFoo); Tests.m:32:7: error: implicit conversion from nullable…
Heath Borders
  • 30,998
  • 16
  • 147
  • 256
11
votes
1 answer

NSError, Swift, and nullability

I'm writing a set of tools in Objective-C that will be used by Swift at some point so I'm using generics and nullability. What am I supposed to do in this situation? - (NSArray * __nullable)foo:(NSError **)error; Currently I'm getting a…
Rob Sanders
  • 5,197
  • 3
  • 31
  • 58
11
votes
1 answer

How to create class methods that conform to a protocol shared between Swift and Objective-C?

I've been learning Swift lately. I decided to write a hybrid Swift/Objective-C app that did compute-intensive tasks using the same algorithm implemented in both languages. The program calculates a large array of prime numbers. I defined a protocol…
Duncan C
  • 128,072
  • 22
  • 173
  • 272
7
votes
1 answer

Can we ensure nullability of `+ (nonnull instancetype)sharedInstance;`?

This is a question on how to gracefully circumvent the nullability of init in NSObject class. So here is a classic objective-c implementation: + (instancetype)sharedInstance { static dispatch_once_t onceToken; static id sharedInstance; …
Cœur
  • 37,241
  • 25
  • 195
  • 267
6
votes
1 answer

Nullability annotation warnings after NS_ASSUME_NONNULL_END

I was working on adding some nullability annotation to part of a header file, and I wanted the block to assume non-null, but pretty much every method afterwards was giving warning saying that I needed to add nullability annotations. Example code for…
NickCE
  • 409
  • 3
  • 7
6
votes
2 answers

What is reason of Warning "Pointer is missing a nullability type specifier"?

+ (UIColor*) getColorWithHexa(NSString*)hexString; : This is a method definition in my class. It's causing a warning. What is cause of similar warnings and how can these be resolved? I am returning an UIColor object, while that question relates to…
Sam Shaikh
  • 1,596
  • 6
  • 29
  • 53
6
votes
2 answers

Conflicting documentation for `[UIView initWithFrame:]`: nullable or nonnull?

Using CLANG_ANALYZER_NONNULL (i.e. -Xclang nullability), I got "Null is returned from a function that is expected to return a non-null value": Using Xcode 7.3 and iOS 9.3 documentation, I checked initWithFrame: and it can return nil: But UIView.h…
Cœur
  • 37,241
  • 25
  • 195
  • 267
5
votes
1 answer

Are _Nullable and _Nonnull useful for anything other than Swift interop?

Does the Objective-C compiler actually warn you if it thinks that you may be passing nil to a parameter marked with _Nonnull? Or is it merely a hint to whatever tool converts between Swift and Objective-C to better deal with Swift optionals?
iosdude
  • 1,131
  • 10
  • 27
4
votes
1 answer

Marking plain -init to have a nullable return type without a warning

I have an ObjC class inheriting from NSObject that wraps some third-party library macros so that I can use their functionality in Swift. The class has an ivar that's a container type from the library. Creation of the container can fail, and if it…
jscs
  • 63,694
  • 13
  • 151
  • 195
4
votes
1 answer

Handle _Nonnull or Nullable with multi-level pointer type

How to associate nullability keywords with multi-level pointer types in following declaration for (NSError **)error? - (void)loadSessionForUserId:(nonnull NSString *)userId error:(NSError **)error { ... } I want to make error nullable and get rid…
lal
  • 7,410
  • 7
  • 34
  • 45
4
votes
1 answer

CLANG_WARN_NULLABLE_TO_NONNULL_CONVERSION removed?

Seems like CLANG_WARN_NULLABLE_TO_NONNULL_CONVERSION was removed in Xcode 8. It provided some really good warnings in case nil was passed to nonnull parameters/return types. Any idea why this was removed and whether there exists a replacement?
fabb
  • 11,660
  • 13
  • 67
  • 111
1
2 3