Reputation: 6522
I am reading through some code and I came across the following statement and it seems to make absolutely no sense. The line is coded below. It takes the first object of an NSMutableArray and then sends the "count" message. What exactly is this doing?
for (int j = 0; j < [[inputData objectAtIndex:0] count]; j++)
//inputData is just an NSMutableArray
Could someone please explain this to me? I have never come across this before. I thought at first maybe it was like a 2-D array scenario where array[x].length is different from array.length, but inputData is just a single NSMutableArray of like 10 numbers.
Upvotes: 1
Views: 2667
Reputation: 2210
If your array(in this case inpuData
) has a key-value matching mechanism, this loop will increment its index but since everytime the condition is the same(the count of [inputData objectAtIndex:0]
will never change ) this will cause an infinite loop
Upvotes: 2
Reputation: 86651
If you are correct that inputData
contains NSNumber
s, then what happens is that the first time through, you get an exception because NSNumber
does not respond to -count
.
The only way that code would do anything else is if [inputData objectAtIndex:0]
responds to -count
. It can be any object that responds to -count
not just an array. Actually, it would also not throw an exception if inputData
was nil
. The expression would then return 0 (sending objectAtIndex:
to nil
returns nil
and sending count
to nil
returns 0).
Upvotes: 0