Determining the number of objects in an NSArray
I am making a book application. To move to the next topic I am using a button. The Button works as it moves to the next topic, but at the end of the file my application gets the message obj_fatal and it crashes. If I knew how many objects there are in my NSArray then the problem will be solved. I am getting the details from a .plist file and storing it in to a array.
开发者_开发百科So if any one knows how, please let me know.
Thanks in advance. Viral.
-[NSArray count]
Alternatively, if you prefer the look and feel of a property, you can do the following, assuming your NSArray is called myArray:
myArray.count
which is the same as @shosti's method, but looks different.
The difference is readability. So you could have:
if ([myArray count] > 0)
or you could have
if (myArray.count > 0)
which probably looks neater.
Both answers are good and correct. Whichever you choose depends on your coding style. As I said, both ways accomplish the same thing and call the same getter method.
Hope that helps.
精彩评论