Waitak Jong
Waitak Jong

Reputation: 87

why adding string to NSMutableArray not work?

why adding string to nsmuarray not work? firstly, i add the a NSDictionary by keypath to the NSMutableArray, its work. after that i want to add one more string to that but its not work.

NSMutableArray *_joinornot;

_joinornot = [[NSMutableArray alloc] init];

NSDictionary *tempobject = [[NSDictionary alloc] init];

_joinornot = [tempobject valueForKeyPath:@"groupid"];

until now everything work.

[_joinornot addObject:@"111"];<----unrecongnized selector sent to instance

Upvotes: 0

Views: 56

Answers (3)

Ronak Chaniyara
Ronak Chaniyara

Reputation: 5435

Try below code:

Before adding object just check for nil.

NSMutableArray *_joinornot;

    _joinornot = [[NSMutableArray alloc] init];

    NSDictionary *tempobject = [[NSDictionary alloc] init];

    _joinornot = [tempobject valueForKeyPath:@"groupid"];

    if (_joinornot==nil) {

        _joinornot = [[NSMutableArray alloc] init];
        [_joinornot addObject:@"111"];
    }
    else{

        [_joinornot addObject:@"111"];
    }

Edit:

May be it's converted to NSArray so it will be no more mutable, try with

_joinornot = [[tempobject valueForKeyPath:@"groupid"] mutableCopy];

Upvotes: 0

Vlad
Vlad

Reputation: 7

Looks like "_joinornot" it's not an NSMutableArray or NSMutable data type, try to see what kind of object it is:

NSLog(@"%@", [_joinornot class]);

If it is not a subclass of Mutable type you can't add objects to him.

Upvotes: 0

Fonix
Fonix

Reputation: 11597

if _joinornot = [tempobject valueForKeyPath:@"groupid"]; returns nil, then your array will be nil, and then you cant call addObject. so maybe add a nil check

Upvotes: 2

Related Questions