B.Saravana Kumar
B.Saravana Kumar

Reputation: 1242

NSSingleObjectArrayI error when JSON Parsing in iOS?

In My code when I am Parse the API I got the error like the __NSSingleObjectArrayI has nil, but is the Array have Values.

Here I Gave my Part of Code..

latitudeArray = [monumentArray valueForKey:@"lat"];
        NSLog(@"latitudeArray is %@",latitudeArray);
        longitudeArray = [monumentArray valueForKey:@"lng"];
        NSLog(@"longitudeArray is %@",longitudeArray);

for (int i = 0; i<3; i++) {

            CLLocation *locationA = [[CLLocation alloc] initWithLatitude:[latStr doubleValue] longitude:[longStr doubleValue]];
            CLLocation *locationB = [[CLLocation alloc] initWithLatitude:[[latitudeArray objectAtIndex:i] doubleValue] longitude:[[longitudeArray objectAtIndex:i] doubleValue]]; ----------> This Line I Got The Crash Error..

            CLLocationDistance distanceInMeters = [locationA distanceFromLocation:locationB];
            NSLog(@"distanceInMeters is %f",distanceInMeters);
            float i  = distanceInMeters/1000;
            NSLog(@"distance between two places is %f KM", i);

            //[locationDistances addObject:distanceInMeters];
        }

What is the meaning of NSSingleObjectArrayI?

*** Terminating app due to uncaught exception 'NSRangeException', reason: '*** -[__NSSingleObjectArrayI objectAtIndex:]: index 1 beyond bounds [0 .. 0]'

Please help me for resolve it.

The latitudeArray has values...

latitudeArray is (
    (
            (
                    (
                            (
                "27.1753210000",
                "27.1798189999",
                "27.1928780000"
            )
        )
    )
)

)

Upvotes: 2

Views: 9181

Answers (3)

gnasher729
gnasher729

Reputation: 52538

First to clear up some confusion: NSSingleObjectArray is just a special case of NSArray for arrays with a single element; happens quite a lot and having a special, smaller object for that case saves space. (There is even a class for empty arrays with a singleton instance; all empty arrays are the same object!). So pretend it is an NSArray.

The exception says clearly that you tried to access an array element that didn't exist. You tried to access the element at index 1, when your array had only one element, so only the element at index 0 could be accessed.

It seems quite obvious that you expect longitudeArray to have three elements, but it has only one. And I bet that monumentArray isn't an array, so don't call it in array.

When you process JSON, you must check that the data you received is what you expected, otherwise your app will end up crashing all over the place if your server doesn't send you exactly what you expect.

Upvotes: 8

KKRocks
KKRocks

Reputation: 8322

Try this :

if (latitudeArray.count == longitudeArray.count) {
        for (int i = 0; i<latitudeArray.count; i++) {

        }
    }

Upvotes: 1

dirtydanee
dirtydanee

Reputation: 6151

[latitudeArray objectAtIndex:i] or [longitudeArray objectAtIndex:i] has not enough items at the time you try to access it at a particular index.

What you can do, to avoid the crash, is before you would access the object at a particular index, you check, if the count if the array is not bigger than the value of i.

 for (int i = 0; i<3; i++) {
        if ([latitudeArray count] > i &&   [longitudeArray count] > i) {
            CLLocation *locationA = [[CLLocation alloc] initWithLatitude:[latStr doubleValue] longitude:[longStr doubleValue]];
            CLLocation *locationB = [[CLLocation alloc] initWithLatitude:[[latitudeArray objectAtIndex:i] doubleValue] longitude:[[longitudeArray objectAtIndex:i] doubleValue]];

            CLLocationDistance distanceInMeters = [locationA distanceFromLocation:locationB];
            NSLog(@"distanceInMeters is %f",distanceInMeters);
            float i  = distanceInMeters/1000;
            NSLog(@"distance between two places is %f KM", i);
        }
    }

Upvotes: 2

Related Questions