13

nonnull works for C functions but not obj-c methods. To be clear, I am suggesting this

- (void)doSomethingWithRequiredString:(NSString * __attribute((nonnil)))requiredString
                                  bar:(NSString *)optionalString);

or (more like nonnull)

- (void)doSomethingWithRequiredString:(NSString *)requiredString
                                  bar:(NSString *)optionalString)
__attribute((nonnil(0)));

I have puzzled over whether or not there is a good technical reason. I understand that clang could only really use the attribute for a compile time check or static analysis, but that seems orthogonal. Is there some strong reason not to have this?

1
  • My suspicion: clang is a huge project. Its developers have better things to do.
    – user529758
    Oct 4, 2013 at 16:05

3 Answers 3

26

You totally can. The only thing you're doing wrong is thinking that method parameters are 0-indexed, when in fact they're 1-indexed (oh, and it's nonnull, not nonnil):

- (void)doSomethingWithRequiredString:(NSString *)requiredString
                                  bar:(NSString *)optionalString
        __attribute((nonnull(1)));

Now when you try to use that:

id thing = ...;
[thing doSomethingWithRequiredString:nil bar:@"42"];

Xcode will warn you with a by saying "Null passed to a callee which requires a non-null argument".

Also, if you leave out the "(1)" portion of the __attribute, it's assumed that the non-nil requirement applies to all parameters.

Clang recognizes the GCC attributes, and GCC's definition of the nonnull attribute is here: http://gcc.gnu.org/onlinedocs/gcc-4.0.0/gcc/Function-Attributes.html#index-g_t_0040code_007bnonnull_007d-function-attribute-1733


Update: As of Xcode 6.3 a cleaner syntax is supported.

In properties and methods the keywords are nullable, nonnull and null_unspecified.

So your method signature would become this:

- (void)doSomethingWithRequiredString:(nonnull NSString *)requiredString
                                  bar:(nullable NSString *)optionalString;
9
  • Many thanks. It was the indexing that got me. That was unexpected.
    – griotspeak
    Oct 4, 2013 at 17:33
  • @griotspeak yeah, that threw me for a loop too. Oct 4, 2013 at 17:35
  • @DaveDeLong is there an easier per–parameter version of this attribute, similar to __unused?
    – danyowdee
    Jan 16, 2014 at 10:42
  • @danyowdee I'm not sure what you mean; I show how to do a "per parameter" attribute above... Jan 16, 2014 at 15:58
  • 1
    @DaveDeLong \o/ those are great news!
    – danyowdee
    Jan 17, 2014 at 23:42
0

In the Xcode 6.3 beta new Objective-C features have been added to express (non)nullability in headers: https://developer.apple.com/swift/blog/?id=22

0

Yes there is.

You may code like this:

- (nullable AAPLListItem *)itemWithName:(nonnull NSString *)name;
- (NSInteger)indexOfItem:(nonnull AAPLListItem *)item;

https://developer.apple.com/swift/blog/?id=25

Your Answer

Reminder: Answers generated by Artificial Intelligence tools are not allowed on Stack Overflow. Learn more

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.