Jeremy
Jeremy

Reputation: 43

Change value in this array

Was wondering how i would change the first value of "Jeremy Arimado" into a different string?

crewData = @[
                @{
                    @"roleNameAr": @"Jeremy Arimado",
                    @"rolePhoneAr":@"123456",
                    },
                @{
                    @"roleNameAr": @"Jeremy Arimado 2",
                    @"rolePhoneAr":@"123456",
                    },
                @{
                    @"roleNameAr": @"Jeremy Arimado 3",
                    @"rolePhoneAr":@"123456",
                    }
                ];

Upvotes: 0

Views: 49

Answers (1)

Gabriele Petronella
Gabriele Petronella

Reputation: 108101

The @[] literal produces an NSArray instance, which is immutable. In the same way @{} produces an NSDictionary, immutable as well.

You have to obtain a mutable copy of the objects, before being able to modify it.

NSMutableArray *mutableCrewData = [crewData mutableCopy];
NSMutableDictionary *mutableCrewMember = [mutableCrewData[0] mutableCopy];
mutableCrewMember[@"roleNameAr"] = @"Foo Bar";
mutableCrewData[0] = mutableCrewMember;
crewData = mutableCrewData;

An alternative would be to directly use NSMutableDictionary and NSMutableArray, but you cannot directly use the literal syntax for that.

Upvotes: 2

Related Questions