how to deep copy multidimensional NSMutableArray correctly? - objective-c

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.

Related

How to add string objects to NSMutableArray

I have an NSMutableArray named randomSelection:
NSMutableArray *randomSelection;
I am then trying to add strings to this array if certain criteria are met:
[randomSelection addObject:#"string1"];
I am then trying to output the string, to determine if it has added it:
NSString *test = [randomSelection objectAtIndex:0];
NSLog(test);
However nothing is being output to the error log and I can't figure out why.
Any help/hints appreciated.
I think you are missing to allocate the memory for array. So try this
NSMutableArray *randomSelection = [[NSMutableArray alloc] init];
[randomSelection addObject:#"string1"];
NSString *test = [randomSelection objectAtIndex:0];
NSLog(test);
NSMutableArray *randomSelection = [[NSMutableArray alloc]init];
[randomSelection addObject:#"string1"];
You need to alloc it first.
First allocate the array using following statement & then objects in it.
NSMutableArray *randomSelection = [[NSMutableArray alloc] init];
[randomSelection addObject:[NSString stringWithFormat:#"String1"]];
[randomSelection addObject:[NSString stringWithFormat:#"String2"]];
NSLog(#"Array - %#", randomSelection);
This will definitely solves your problem.
Just allocate your NSMutableArray. You'll get solved your problem.
Try this:
NSMutableArray *randomSelection = [[NSMutableArray alloc]init];
[randomSelection addObject:#"string1"];
Swift :
var randomSelection: [AnyObject] = [AnyObject]()
randomSelection.append("string1")
let test: String = randomSelection[0] as! String
print(test)
OR
let array : NSMutableArray = []
array.addObject("test String")
print(array)

How to put elements from NSMutableArray into C char?

I have a NSMutableArray and I need to sort its elements into separate C char.
How to accomplish that? Or is it better to put the elements into NSStrings?
I've tried this, and it crashes when I try to log the result:
NSMutableArray *array = [[NSMutableArray alloc] init];
... do something to fill the array ...
NSString *string;
string = [array objectAtIndex:0];
NSLog(#"String: %#", string);
*I really prefer putting the elements of the array into C char, because I already have some woking code using char instead of NSStrin*g.
Thanks!
Dont see any specific reason to convert NSString to C chars. To sort an array full of NSStrings try this method -
NSMutableArray *array = [[NSMutableArray alloc] init];
sortedArray = [array sortedArrayUsingSelector:#selector(localizedCaseInsensitiveCompare:)];
NSString *string = [sortedArray objectAtIndex:0];
NSLog(#"String: %#", string);

How to return an NSMutableArray from an NSSet

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

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.

Objective-C - How to compare arrays and extract the difference?

Possible duplicate: comparing-two-arrays
I have two NSArray and I'd like to create a new Array with objects from the second array but not
included in the first array.
Example:
NSMutableArray *firstArray = [NSMutableArray arrayWithObjects:#"Bill", #"Ben", #"Chris", #"Melissa", nil];
NSMutableArray *secondArray = [NSMutableArray arrayWithObjects:#"Bill", #"Paul", nil];
The resulting array should be:
[#"Paul", nil];
I solved this problem with a double loop comparing objects into the inner one.
Is there a better solutions ?
[secondArray removeObjectsInArray:firstArray];
This idea was taken from another answer.
If duplicate items are not significant in the arrays, you can use the minusSet: operation of NSMutableSet:
NSMutableArray *firstArray = [NSMutableArray arrayWithObjects:#"Bill", #"Ben", #"Chris", #"Melissa", nil];
NSMutableArray *secondArray = [NSMutableArray arrayWithObjects:#"Bill", #"Paul", nil];
NSSet *firstSet = [NSSet setWithArray:firstArray];
NSMutableSet *secondSet = [NSMutableSet setWithCapacity:[secondArray count]];
[secondSet addObjectsFromArray:secondArray];
[secondSet minusSet:firstSet]; // result is in `secondSet`
I want to compare images from two NSArray.
One Array, I was getting from Core Database. Second I have constant array objects.
I want to know that object of second array is present in Core database or not.
Here is code which i used.
// All object from core data and take into array.
NSFetchRequest *fetchRequest = [[NSFetchRequest alloc]initWithEntityName:#"student"];
NSArray *dbresult = [[NSArray alloc]init];
NSError *error;
#try {
dbresult = [context executeFetchRequest:fetchRequest error:&error];
}
#catch (NSException *exception) {
NSString *logerror = [NSString stringWithFormat:#"error in fetching Rooms from coredata = %#",exception.description];
NSLog(logerror)
}
#finally {
}
/*
Get Unused images from list
*/
NSMutableArray *usedImages = [dbresult valueForKey:#"roomImageLocalPath"];
NSMutableSet *fSet = [NSMutableSet setWithArray:usedImages];
NSMutableSet *sSet = [NSMutableSet setWithCapacity:[newImages count]];
[sSet addObjectsFromArray:newImages];
[sSet minusSet:fSet];
NSArray *unusedImages = [secondSet allObjects];
NSLog(#"unusedImages %#",unusedImages);