ios adding array of dictionaries to another array of dictionaries - iphone

I'm trying to add the dictionaries I have in one array to another array with dictionaries so I can have one array with the dictionaries of both arrays.
I have tried this:
[arrayOne arrayByAddingObjectsFromArray:arrayTwo];
But I'm getting the following error:
-[__NSDictionaryM arrayByAddingObjectsFromArray:]: unrecognized selector sent to instance 0x75ba1a0

- (NSArray *)arrayByAddingObjectsFromArray:(NSArray *)otherArray;
This function returns NSArray not NSDictionary, I guess in your code arrayOne is NSDictionary not NSArray right?
In Xcode source check warning shown for below line
[arrayOne arrayByAddingObjectsFromArray:arrayTwo];
To copy dictionary refer this: Copying the contents of an NSMutableDictionary into another NSMutableDictionary?

Related

coredata how to change default NSSet to NSMutableArray

CoreData one-to-many default to generate NSSet ,how to change NSSet to NSMutableArray?I try to change it manually,but get error:
_NSFaultingMutableSet filteredArrayUsingPredicate:]: unrecognized selector sent to instance 0x1ed35e40'
NSSet has the method allObjects, which returns an NSArray.
To get an NSMutableArray you could do this:
NSMutableArray *array = [NSMutableArray arrayWithArray:myCoreDataObject.mySet.allObjects];
Note: order is not guaranteed to be the same every time (sets aren't ordered). If order is important to you, consider an NSOrderedSet instead.
See also the docs on NSSet:
https://developer.apple.com/library/mac/#documentation/Cocoa/Reference/Foundation/Classes/NSSet_Class/Reference/Reference.html
PS:
The reason you're getting said error:
NSSet (or _NSFaultingMutableSet for that matter) doesn't have a method called filteredArrayUsingPredicate.

NSMutableArray removeObjectAtIndex

I'm getting the following error when removing from my NSMutableArray
-[__NSArrayI removeObjectAtIndex:]: unrecognized selector sent to instance 0x1cdced10
2011-07-13 00:33:14.333 MassText[1726:707] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[__NSArrayI removeObjectAtIndex:]: unrecognized selector sent to instance 0x1cdced10'
However right before I remove, I print out the array and the index. Neither are nil and I have no reason to believe why this error would be happening. Any ideas?
I had this problem. Mine was that I accidentally used type casting like this.
NSMutablearray * myarray = [[NSMutableArray alloc] init];
myarray =(NSMutableArray*) [mydictionary allkeys];
This will work for some time.. but if you are in a tight and large loop this tend to fail.
I changed my code to
NSMutableArray * myarray= [[NSMutablearray alloc] initWithArray:[mydictionary allKeys]];
The object is an NSArray, not an NSMutableArray.
You are calling removeObjectAtIndex on a NSArray instance. We can see clearly by your crash log.
At this point, four smart people (not including myself) have pointed out that you're sending -removeObjectAtIndex: to an object that thinks it's an immutable array. This would be a good time to start wondering why the array is immutable when you previously thought it was mutable. If you post some code that shows how the array is created, someone here will probably be able to show you what's going wrong.
One way that you can end up with an immutable array when you thought you had a mutable one is to copy a mutable array. For example, you might have a property:
#property (copy) NSMutableArray *myArray;
Perhaps you then create a mutable array, add some objects, and assign it to your property:
NSMutableArray *tempArray = [NSMutableArray array];
[tempArray addObject:#"You say goodbye"];
[tempArray addObject:#"I say hello"];
self.myArray = tempArray;
Now, does tempArray point to a mutable array or an immutable array? I haven't tested recently, but I'm pretty sure that you get an immutable array. You definitely get an immutableArray if you say:
NSMutableArray *foo = [tempArray copy];
So, start looking for places in your code where your array pointer is reassigned. After all, if your pointer really did point to a mutable array, it'd be awfully hard to explain the exception that you're getting.
The error says that you are trying to call the removeObjectAtIndex selector on an NSArray, which won't respond to that selector.
Make sure the array is really an NSMutableArray, not an NSArray.
I had the same problem, and it was because of the use of the copy method. I made one on my own returning a NSMutableArray* and it worked.

Using NSDictionaries

I have a number of NSString objects I want to save into an NSDictionary.
How is an NSDictionary initialized with the keys and do the values need to be set there and then or can the dictionary be setup using just the keys and the objects later set?
To start with here's a link to the NSDictionary documentation, and NSMutableDictionary
You can create a NSMutableDictionary with just keys by passing [NSNull null] as the object for that key. It's important to use a mutable dictionary here otherwise you won't be able to change the dictionary after you've created it, and a dictionary that just has keys instead of objects is of little use.
That is what NSNull is for: to provide null values for collection types that don't allow nils.
A dictionary can be initialized both as empty or as a list of (value,key) pairs.
Initializing the dictionary just with keys and not values is useless/meaningless in Objective-C, since it will be equivalent to a void initialization.
If you want to create a dictionary and want to be able to set objects later, you have to use a NSMutableDictionary, that has not a static initialization of its data, through its setObject:forKey: method.

problem in nsuserdefaults

hii every one i have stil problem in nsuser defaults i'll tell the scenerio in detail
First i have diclared nsmutable array in appDelegate and set it in NSUserDefaults With For Key#"abc"
In FirstView Controller i first fetch the array from NSUserDefaults and save its values in NSMutable Array
When a Click say abcButton i have create a dictionary and adding values in it like
[abcDictionar setObject:[[abcMutableArray objectAtIndex:indexPath.row] objectForKey:#"abc"] forKey:#"abc"];
When i added all values in NSDictionary Then i add NSDictionary in NSMutable Array
like This
[abcMutableArray addObject:abcDictionary];
Then i save it NSUserDefaults
It Give me Exception in Point 4
When i add nsdictionary in Point One It All work fine but data in array is ambigous and it raise exception when am going to display it in tableview
if any one has some idea then let me know thanks in advance...:)
NSUserDefaults does not store mutable objects, only immutable ones. When you retrieve objects from it you must cast/copy them into mutable objects if you wish to mutate them.
Also be aware that:
The NSUserDefaults class provides convenience methods for accessing common types such as floats, doubles, integers, Booleans, and URLs. A default object must be a property list, that is, an instance of (or for collections a combination of instances of): NSData, NSString, NSNumber, NSDate, NSArray, or NSDictionary. If you want to store any other type of object, you should typically archive it to create an instance of NSData.
Which is from the NSUserDefaults documentation.

Application crash problem in iPhone

I have used NSMutable Dictionary and NSMutable Array. The datas are to be stored and retrieved from plist(Documents Directory) using NSArray of NSMutable Dictionary.
Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: '-[__NSCFArray removeObjectAtIndex:]: mutating method sent to immutable object'
Please Guide me why its happened?.
Thanks!
It might help if you post the exact code that causes. My guess would be that while you are using NSMutableDictionary, the call to valueForKey: returns to you a non-mutable NSArray, and you think it is returning you an NSMutableArray instance. Note that mutable arrays and dictionaries allow you to manipulate the collection of items inside them, but do not guarantee you that those items themselves are mutable. For example, if you check the Property List Programming Guide: Reading and Writing Property-List Data, you will notice the following example:
If you load the property list with
this call:
NSMutableArray * ma = [NSMutableArray arrayWithContentsOfFile:xmlFile];
ma is a mutable array with immutable
dictionaries in each element. Each key
and each value in each dictionary are
immutable.
If you need explicit control over the mutability of the objects at each level, use propertyListFromData:mutabilityOption:format:errorDescription:
You can also create an explicit NSMutableArray copy from the NSArray you got from the NSMutableDictionary.
you said you are retrieving the array as a NSArray and not casting it to a NSMutableArray before you attempt to remove an object for it. This causes an error since you can't remove an object from an NSArray
Suppose [something dictionaryValue] returns an immutable dictionary, and you want a mutable version of that dictionary. It is not enough to say:
NSMutableDictionary *d = [something dictionaryValue];
This merely tells the compiler that d is an NSMutableDictionary, but really it's the same immutable dictionary you got from [something dictionaryValue]. Instead, you need to create a new, mutable copy of the dictionary:
NSMutableDictionary *d = [NSMutableDictionary dictionaryWithDictionary:
[something dictionaryValue]];
Similarly, use [NSMutableArray arrayWithArray:...] for arrays.