0

This is my code:

NSMutableArray* notifications = [NSMutableArray arrayWithObjects:myObject.dictionary, nil];

After creating the NSMutableArray I do this:

NSData *jsonData = [NSJSONSerialization dataWithJSONObject:notifications options:NSJSONWritingPrettyPrinted error:&writeError];

How can I add other objects to the NSMutableArray notification?

I know I can do something like:

NSMutableArray* notifications = [NSMutableArray arrayWithObjects:object1.dictionary, object2.dictionary, object3.dictionary, nil];

but I want to add them after the creation of the NSMutableArray.

myObject contains this:

-(NSDictionary *)dictionary {
    return [NSDictionary dictionaryWithObjectsAndKeys:self.name,@"name",self.category,@"category",self.note, @"note",self.dueDate, @"dueDate",self.creationDate, @"creationDate", nil];}
5
  • Does this answer your question? How to append values to an array in Objective-C Commented Aug 21, 2020 at 12:07
  • No, because in this case I need to add an object using arrayWithObject:object.dictionary.. I update my question Commented Aug 21, 2020 at 12:15
  • I need to add an Object to notification, but as you can see, when I create the NSMutableArray notification I use arrayWithObjects:myObject.dictionary. How can I add another one in the same way? Commented Aug 21, 2020 at 13:02
  • 1
    Will this work: [notifications addObjectsFromArray: [NSMutableArray arrayWithObject: object.dictionary]] ? Commented Aug 21, 2020 at 13:15
  • Fantastic. If u write as a post I give u the accepted answer Commented Aug 21, 2020 at 13:18

1 Answer 1

0

You can add one object as an array to an existing array as follows:

[notifications addObjectsFromArray: [NSArray arrayWithObject: object.dictionary]];

Alternatively, instead of arrayWithObject, you can also use the literal notation:

[notifications addObjectsFromArray: @[object.dictionary]];

You can add even more than one object at a time:

[notifications addObjectsFromArray: @[object1.dictionary, object2.dictionary]];
Sign up to request clarification or add additional context in comments.

1 Comment

Hi - I'd simply use [NSArray arrayWithObject:... here, mutable is not required.

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.