Getting Index of an Object from NSArray?

后端 未结 7 1470
没有蜡笔的小新
没有蜡笔的小新 2020-12-12 23:17

i am trying to get index of an array through indexOfObject method as follows but when i try to log the value to test the index i get a garbage value.. for t

相关标签:
7条回答
  • 2020-12-13 00:21

    Folks,

    When an object is not found in the array the indexOfObject method does NOT return a 'garbage' value. Many systems return an index of -1 if the item is not found.

    However, on IOS - because the indexOfObject returns an UNSIGNED int (aka NSUInteger) the returned index must be greater than or equal to zero. Since 'zero' is a valid index there is no way to indicate to the caller that the object was not found -- except by returning an agreed upon constant value that we all can test upon. This constant agreed upon value is called NSNotFound.

    The method:

    - (NSUInteger)indexOfObject:(id)anObject;
    

    will return NSNotFound if the object was not in the array. NSNotFound is a very large POSITIVE integer (usually 1 minus the maximum int on the platform).

    0 讨论(0)
提交回复
热议问题