0

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
10
  • Where did you actually allocate an NSMutableArray and add it to the dictionary? Commented Jun 8, 2016 at 8:20
  • i have initialize the NSMutableArray Commented Jun 8, 2016 at 8:21
  • [_joinornot addObject:@"111"]; just this line is not work.. Commented Jun 8, 2016 at 8:23
  • what's error you got? Commented Jun 8, 2016 at 8:25
  • unrecongnized selector sent to instance Commented Jun 8, 2016 at 8:29

3 Answers 3

2

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

Sign up to request clarification or add additional context in comments.

1 Comment

i have check that its not nil before addobject
0

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.

1 Comment

If you want to add the string maybe this help: _joinornot = [[NSMutableArray alloc] initWithArray: _joinornot]; just before add the string
0

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];

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.