Returns NSNotFound for NSUInteger?

I am working on a category for NSArray , and one of the functions I implemented is for returning the number of rows (or not numbers) in an array. All this works fine, but my question is to use NSNotFound as a return when the number of input arrays is zero.

Apple docs:

NSNotFound is commonly used by various methods and functions that look up elements in serial data and return indices, such as characters in a string object or ids in an NSArray object.

This makes sense if I have to use indexOfObject: for an element that was not in the array. But, uses NSNotFound in the context shown below, the correct use? Of course, I could just return zero and call it day, but it would be useful to see 2147483647 and immediately find out where the problem is.

 - (NSUInteger)countOfStringsInArray { NSUInteger currentCount = 0; if ([self count] > 0) { for (id obj in self) { if (![obj integerValue]) { currentCount ++; } } return currentCount; } return NSNotFound; } 
+7
source share
4 answers

No, not in this case. The method in the example returns the score. You would consider using / returning an NSNotFound (for example) method that returns an index, where 0 also a valid index and therefore a distorting result.

So yes, you should just return 0, and the return type should be unsigned (NSUInteger) ", since NSNotFound should not be related to counting. It really covers your affairs, regardless of the magic value.

If you prefer to know where the problem is (and I do it myself quite often), I find it preferable to just consider this a programmer error to call the method when the collection is empty.

+5
source

I do not think that the account can be called NSNotFound . The score can only be 0 or more. In my opinion, it can never be "not found . " If so, then [NSArray count] should return NSNotFound when the array is empty or the array is nil, right? In short, I believe that using NSNotFound in this particular case is not a standard way. At least an apple would never use it that way.

You can probably use zero as the default case, otherwise you may need to define your own enumeration to represent it. NSNotFound is only useful if you cannot use zero as the default value (for these cases, -1 can be set to the default value if it is acceptable, or NSNotFound can be used, since this means NSIntegerMax ).

+2
source

I can see where you are from, especially given the proposed use of NSNotFound. However, for a subsequent developer who inherits this code, I do not think that your proposed implementation would be intuitive.

I would recommend using the return value of NSRange instead. In Apple’s definition for Apple, NSRange is "a structure used to describe part of a series, such as characters in a string or objects in an NSArray." This is exactly what you have.

In particular, the NSRange location member may be the index of the first row you find in the array, and the number of rows found may be the length term.

And like other methods that return NSRange (e.g. NSString: rangeOfString), you can set the location to NSNotFound if no rows are found.

Hope this helps.

+1
source

NSNotFound for unsigned ints so you should return it from the NSInteger method, as it will be abbreviated and will be incorrect (potentially)

which said: ** your method "countOfStringsInArray" should return n NSUInteger (same as nsarray.count)

0
source

All Articles