How to return an NSMutableArray from an NSSet - objective-c

I'm able to put the contents of an NSSet into an NSMutableArray like this:
NSMutableArray *array = [set allObjects];
The compiler complains though because [set allObjects] returns an NSArray not an NSMutableArray. How should this be fixed?

Since -allObjects returns an array, you can create a mutable version with:
NSMutableArray *array = [NSMutableArray arrayWithArray:[set allObjects]];
Or, alternatively, if you want to handle the object ownership:
NSMutableArray *array = [[set allObjects] mutableCopy];

I resolved crashing by using NSMutableArray's method 'addObjectsFromArray' to assign all NSSet objects to NSMutableArray like:
[mutableArray addObjectsFromArray:[cg_Schedule.schedule_Days allObjects]];
Hope this will helps you.

For an ordered set use:
NSArray *myArray = [[myOrderedSet array] mutableCopy];

Related

Most efficient way to create an NSMutableArray from NSArray

Which of these, if either, is more efficient?
NSMutableArray *array = [NSMutableArray arrayWithArray:#[#1, #2]];
or
NSMutableArray *array = [#[#1, #2] mutableCopy];
Or are these the same internally?
The two options you provide both first create an NSArray and then create an NSMutableArray from the NSArray so there is essentially no difference.
There is a third option that would be ever so slightly better in this case:
NSMutableArray *array = [NSMutableArray arrayWithObjects:#1, #2, nil];
This doesn't create an intermediate NSArray like your other two options.
While using ARC there is no difference in your example. But if you would use NSArray variable instead of static #[#1, #2], in case of this array being nil the first would return you empty array and the second one will return you nil.

how to deep copy multidimensional NSMutableArray correctly?

Hi this question got asked before: How do you deep copy a multidimensional NSMutableArray and its contents?
but the answer didn't work for me.
I tried:
NSMutableArray *copy = [[NSMutableArray alloc] initWithArray:multiDimArray copyItems:YES];
and:
NSMutableArray *mutableArray = [multiDimArray mutableCopy];
NSMutableArray *copy = [[NSMutableArray alloc] initWithArray:mutableArray copyItems:YES];
The copying works but when I try to replace an object I get the error:
-[__NSArrayI replaceObjectAtIndex:withObject:]: unrecognized selector sent to instance 0x1073b1790
Am I doing anything wrong or is it just not possible to deep copy a multidimensional array without its elements becoming immutable?
Is there a workaround for this?
Thanks
Try
NSMutableArray *mutableArray = [multiDimArray mutableCopy];
replace with
NSMutableArray *mutableArray = [[NSMutableArray arrayWithArray:multiDimArray] mutableCopy];
I got it to work with this:
NSMutableArray *originalArray = [NSMutableArray new];
NSMutableArray *copyArray = [NSMutableArray new];
for(NSArray *subarray in originalArray) {
NSMutableArray *subCopy = [NSMutableArray new];
for (id item in subarray){
[subCopy addObject:[item copy]];
}
[copyArray addObject: subCopy];
}
I did it for a two dimensional array. If there are more dimensions you probably have to add an extra for loop for every dimension.

mutating method sent to immutable object error when using mutable array functions

My application always popping 'mutating method sent to immutable object'
My dictionary and the array already declare to be a mutable one but seems it doesn't help
I have tried the replaceObjectAtIndex function it also didn't work.
NSString* plistpath = [[NSBundle mainBundle] pathForResource:#"data" ofType:#"plist"];
NSMutableDictionary* dictionary = [NSMutableDictionary dictionaryWithContentsOfFile:plistpath];
NSMutableArray* Array1 = [dictionary objectForKey:#"Array1"];
[Array1 removeObjectAtIndex:num];
[Array1 insertObject:#"1" atIndex:num];
return Array1;
thanks
Try:
NSArray *array = [dictionary objectForKey:#"Array1"]; // Immutable
NSMutableArray * Array1 = [NSMutableArray arrayWithArray:array]; // Mutable
// ...
Or as Julien noted:
NSArray *array = [dictionary objectForKey:#"Array1"]; // Immutable
NSMutableArray * Array1 = [array mutableCopy]; // Mutable
If the right hand side does not return an NSMutableArray (NSArray instead) you try to mutate an in mutable object.
NSMutableArray* Array1 = [dictionary objectForKey:#"Array1"]; // NSArray

Add NSStrings to mutable array

I created a mutable array and I have two NSString variables. Now I want to add these two NSStrings to my array. How is this possible? Thanks.
Use the addObject function of you NSMutableArray.
eg.
[myNSMutableArray addObject:myString1];
[myNSMutableArray addObject:myString2];
Jhaliya's answer is correct. +1 vote.
I added a immutable version so you can see the difference. If you dont want to remove or add more objects (NSStrings) to your container, I would recommend using an Immutable version.
Mutable version:
NSMutableArray *mutableArray = [[NSMutableArray alloc] init];
NSString *string_one = #"One"];
[mutableArray addObject:string_one];
//Or
[mutableArray addObject:#"Two"];
NSLog(#"%#", mutableArray);
Immutable version
NSArray *immutableArray = [NSArray arrayWithObjects:#"One", #"Two", nil];
NSLog(#"%#", immutableArray);
You can add at NSMutableArray allocation.
Like :
NSMutableArray *test = [NSMutableArray arrayWithObjects:#"test1",#"test2",nil];

How add data from NSMutableString into NSArray?

is it an possible to add a value from an NSMutableString into an NSArray? Whats the snippet?
Actually, Mike is wrong. If you want to instantiate an NSArray with a single NSMutableString object, you can do the following:
NSMutableString *myString; //Assuming your string is here
NSArray *array = [NSArray arrayWithObject:myString];
There is no arrayWithElements in NSArray (see NSArray documentation)
If you want to instantiate an NSArray with a single NSMutableString object, you can do the following:
NSString *myString; //Assuming your string is here
NSArray *array = [NSArray arrayWithObjects:myString,nil];
Note that NSArray will be immutable - that is, you can't add or remove objects to it after you've made it. If you want the array to be mutable, you'll have to create an NSMutableArray. To use an NSMutableArray in this fashion, you can do the following:
NSString *myString; //Assuming your string is here
NSMutableArray *array = [[NSMutableArray alloc] init];
[array addObject:myString];
NSArray is immutable, so you cannot add values to it. You should use NSMutableArray in order to do that with the addObject: method.
NSMutableString *str = ...
NSMutableArray *array = [[NSMutableArray alloc] init];
[array addObject:str];
// You must use NSMutableArray to add Object to array
NSMutableArray *tableCellNames;
// arrayWithCapacity is a required parameter to define limit of your object.
tableCellNames = [NSMutableArray arrayWithCapacity:total_rows];
[tableCellNames addObject:title];
NSLog(#"Array table cell %#",tableCellNames);
//Thanks VKJ
An elegant solution would be this:
NSMutableString *str; //your string here
NSArray *newArray = #[str];
Using the new notation, it's a piece of cake.