0

I have a NSMutableArray *allObject, my allObject have 22 objects inside.

And now I want to get 10 objects when I click button More.

I am using:

NSArray *arrrTemp = [arrObject subarrayWithRange:NSMakeRange(from_index, 10)];

1st, I got 10 object from allObject

2nd, I got 10 next object from allObject

It's OK.

But, 3rd: It's crash app. I think subarrayWithRange:NSMakeRange(from_index, 10) ---> 10 is problem.

How to I can resolve this problem?

rmaddy
  • 314,917
  • 42
  • 532
  • 579
Mr.Boy
  • 3
  • 3
  • You can't hardcode a length of 10 in the `NSMakeRange`. You need to check how many objects are left. – rmaddy Dec 15 '15 at 19:49
  • my `allObject` have 22 object and I am using subarrayWithRange to arrrTemp, How to I can check how many objects are left. – Mr.Boy Dec 15 '15 at 19:52

1 Answers1

0

You need to check if there are at least 10 objects left.

NSInteger length = MIN(10, arrObject.count - from_index);
NSRange range = NSMakeRange(from_index, length);
NSArray *arrrTemp = [arrObject subarrayWithRange:range];
rmaddy
  • 314,917
  • 42
  • 532
  • 579