Search code examples
objective-cnsmutablearray

NSRangeException for NSMutableArray regarding objectAtIndex: 0


I have a strange (at least to me it is) issue where my NSMutableArray seems to think that objectAtIndex:1 doesn't exist when it clearly does from what I can see in the console output.

I have provided the array and conflicting code. Just so you know, this is in a UITableView cellForRowAtIndexPath

-- Interface
@property (nonatomic, strong) NSMutableArray *packages;

-- viewDidLoad

packages = [[NSMutableArray alloc] init];

NSMutableArray *package1 = [[NSMutableArray alloc] init];

[package1 addObject:@[@"iPhone Cases", @"9928898669"]];

[packages addObject:package1];

NSMutableArray *package2 = [[NSMutableArray alloc] init];

[package2 addObject:@[@"RGB Controller", @"33A4Q0446691"]];

[packages addObject:package2];

NSLog(@"Packages Array: %@", packages);

-- UITableViewCell

NSMutableArray *package = [packages objectAtIndex:indexPath.row];
NSLog(@"%@", package);
NSString *packageName = [NSString stringWithFormat:@"%@", [package objectAtIndex:0]];
NSString *trackingNumber = [NSString stringWithFormat:@"%@", [package objectAtIndex:1]];


cell.appName.text = packageName;
cell.appVersion.text = trackingNumber;


-- Console Output
2019-03-14 19:45:35.942580+1100 SilentParcel[28937:961776] Packages Array: (
        (
                (
            "iPhone Cases",
            9928898669
        )
    ),
        (
                (
            "RGB Controller",
            33A4Q0446691
        )
    )
)
2019-03-14 19:45:35.975358+1100 SilentParcel[28937:961776] (
        (
        "iPhone Cases",
        9928898669
    )
)

Solution

  • Thank's to this comment (NSRangeException for NSMutableArray regarding objectAtIndex: 0)

    My issue was solved. I still do not understand why or how this fixed it but it did. So thank you @Larme