Fady E
Fady E

Reputation: 346

In Objective-C, How to initialize NSMutableArray with objects in another NSMutableArray using tableView reference

Developing for MacOS, I have an NSMutableArray namesArray[] that contains 3 String objects. namesArray[] is represented in a tableView, where user can select multiple cells, each cell represents a single object. I am trying to initialize a second NSMutableArray savedNamesArray[], and add objects from the original namesArray[] based on the selected cells in my tableView using this method:

NSMutableArray *savedNamesArray = [[NSMutableArray alloc] initWithObjects:[namesArray objectsAtIndexes:[_tableView selectedRowIndexes]], nil];

The problem is, no matter how many objects I select, only one gets added to the new NSMutableArray. Any suggestions?

Upvotes: 1

Views: 53

Answers (1)

Willeke
Willeke

Reputation: 15589

You're adding one object, an array, to savedNamesArray. Use initWithArray: instead.

NSMutableArray *savedNamesArray = [[NSMutableArray alloc] initWithArray:[namesArray objectsAtIndexes:[_tableView selectedRowIndexes]]];

Upvotes: 3

Related Questions