In Xcode 7, I'm getting this warning:
Null passed to a callee that requires a non-null argument
.. from this nil initialization of a NSMutableArray...
sectionTitles = [[NSMutableArray alloc] initWithObjects:nil];
I've found that I should be using removeAllObjects
instead.
[sectionTitles removeAllObjects];
However, this doesn't allow me to evaluate a sectionTitles.count == 0
. I did try sectionTitles == nil
, however unless I use iniWithObjects
I can't add objects later on.
I need to set the array to nil or zero, when I refresh my datasource, when there's no records. I don't seem to be able to use addObject
to add items unless I've used initWithObjects
.
Passing non-null parameters is only partly the answer.
The new Objective-C nullability annotations have huge benefits for code on the Swift side of the fence, but there's a substantial gain here even without writing a line of Swift. Pointers marked as
nonnull
will now give a hint during autocomplete and yield a warning if sent nil instead of a proper pointer.
Read NSHipster comprehensive article.
In oder to take advantage of the same contract in your own code, use nonnull
or nullable
:
Obj-C
- (nullable Photo *)photoForLocation:(nonnull Location *)location
Why don't you try:
sectionTitles = [[NSMutableArray alloc] init];
or any of the following:
sectionTitles = [[NSMutableArray alloc] initWithCapacity:sectionTitles.count];
sectionTitles = [NSMutableArray new];
sectionTitles = [NSMutableArray array];
sectionTitles = [NSMutableArray arrayWithCapacity:sectionTitles.count];
maybe some silly ones:
sectionTitles = [NSMutableArray arrayWithArray:@[]];
sectionTitles = [@[] mutableCopy];
There are lots of ways to create empty mutable arrays. Just read the doc.
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