Reputation: 3675
Is there any difference between this 2?
int count = 0;
for (UIView *view in scrollView.subviews) {
NSLog(@"%d < %d", [json[@"images"] count] - 1, count);
// Output: -1 < 0
if ([json[@"images"] count] - 1 < count) break;
}
and
int count = 0, maxIndex = [json[@"images"] count] - 1;
for (UIView *view in scrollView.subviews) {
NSLog(@"%d < %d", maxIndex, count);
// Output: -1 < 0
if (maxIndex < count) break;
}
What I've facing just now was, the first solution didn't break
the loop, whereas the second solution did.
Is there any reason behind?
Upvotes: 0
Views: 62
Reputation: 2176
Thats because count
is NSUInteger
property. Therefore it will never be -1
in your case.And in your second case you're assigning maxIndex
to int
, which then gives you -1
.
So try this to clearly understand whats happening.
int count = 0;
NSUInteger maxIndex = [json[@"images"] count] - 1;
for (UIView *view in scrollView.subviews) {
NSLog(@"%d < %d", maxIndex, count);
// Output: -1 < 0
if (maxIndex < count) break; //This will not break either as maxIndex will never be `-1`
}
Also,in your NSLog
your using %d
which is format specifier for type int
, try %lu or %lx
Hope this helps
Upvotes: 3