jaytrixz
jaytrixz

Reputation: 4079

How to access array's index within another array in Objective-C

I have this array and I want to access the first index inside another array.

(
    (
        1021,
        "String1,
        "<null>",
        "name1, name2",
        P,
        "String2",
        "Link1",
        "String3",
        "String4"
    ),
    (
        1025,
        "String1",
        "<null>",
        "name1, name2"
        P,
        "String2",
        "Link1",
        "String3",
        "String4"
    )
)

I tried to NSLog using this code:

NSLog(@"ID: %@", [[array objectAtIndex:0] objectAtIndex:0]);

But it doesn't work. It gives me an error saying:

-[__NSCFString objectAtIndex:]: unrecognized selector sent to instance

I just want to log the value 1021 in the first array of the first array.

Upvotes: 0

Views: 1028

Answers (2)

JRG-Developer
JRG-Developer

Reputation: 12663

This error indicates that the first object in array is an NSString.

Does any code add a string to said array?

If the first object in array were a dictionary, your code would be okay.

Upvotes: 1

dreamlax
dreamlax

Reputation: 95315

The problem you are facing is that either array or the first element in array is a string.

Separate it out and step through with a debugger to make sure the array is being loaded and accessed correctly:

NSArray *array = [NSArray /* load from somwhere */];
NSLog(@"%@", array);

NSArray *innerArray = [array objectAtIndex:0];
NSLog(@"%@", innerArray);

NSNumber *objectId = [innerArray objectAtIndex:0];
NSLog(@"%@", objectId);

Upvotes: 6

Related Questions