-2

So I have NSArray that contains objects like {117, 22}{http://t.co/7l3oiMDQt3} and I want to get 117 and 22 I tried to use ObjectAtIndex: and the app was crashed and then added this:

if(array.count > 0) {                                           
   NSLog(@"%@",[array objectAtIndex:1]);
}

and still the app crashes.

is there's any other way to get the NSRange location and length from the array?


EDIT:

The objects inside the array is NSDataDetector that gets the links form a string.

it says this when it crashes:

 *** Terminating app due to uncaught exception 'NSRangeException', reason: '*[__NSArrayM objectAtIndex:]: index 1 beyond bounds [0 .. 0]'
***
Jon Kayin
  • 5
  • 3
  • so what...? what's an "object like {117, 4}{said}`"? Did you use a debugger to investigate the reasons your app crashed? – The Paramagnetic Croissant Aug 23 '14 at 00:06
  • 1
    `objectAtIndex:` does not often crash without a reason. Are you sure your array contains two objects? – The Paramagnetic Croissant Aug 23 '14 at 00:14
  • ^ what he said.. Try NSLog(@"%@", array[0]); – Mike Aug 23 '14 at 00:15
  • When you say, descriptively "the app crashes", what **exactly** does it tell you is the reason for termination? We're not mind readers. – Tommy Aug 23 '14 at 00:18
  • @Tommy sorry about that I added what it says when it crashes – Jon Kayin Aug 23 '14 at 00:29
  • @JonKayin well that's it. Your array contains 1 element, at index 0. You are trying to access the **second** element, at index 1. That's incorrect. (Also, you should have pasted the error message [which is informative and kind of obvious...] into Google **days before** falling back to asking here. Stack Overflow is full of solutions to this very same problem. Don't be lazy.) – The Paramagnetic Croissant Aug 23 '14 at 00:30
  • possible duplicate of [NSArray out of bounds check](http://stackoverflow.com/questions/9715261/nsarray-out-of-bounds-check) – The Paramagnetic Croissant Aug 23 '14 at 00:32
  • @TheParamagneticCroissant the possible duplicate is not solving my problem. – Jon Kayin Aug 23 '14 at 00:37
  • 1
    As others have stated, the crash is because even with the bounds check, you are accessing an array element that is out of bounds. objectAtIndex:0 would be correct for the bounds check you have implemented. – quellish Aug 23 '14 at 00:54
  • have you checked your data carefully. I think it isn't NSArray type – Huy Nghia Sep 01 '14 at 00:30

2 Answers2

1

Your check can still be out of the array range :

if(matc.count > 0) {
NSLog(@"%@",[array objectAtIndex:0]); //not 1 }

Zigglzworth
  • 6,645
  • 9
  • 68
  • 107
0

You should never arbitrarily check an index in an array without knowing the exact length. Whenever you're dealing with an array with a varying length, you should do something like this to prevent trying to access an index that doesn't exist:

for (NSInteger index = 0; index < array.count; index++) {
    NSLog(@"%@", array[index]);
}

You'll never have an out of bounds exception doing the above.

Mike
  • 9,765
  • 5
  • 34
  • 59