1

How do you compare 2 NSMutableArray ?

Different people might say this is a duplicate question, but i haven't found a solution to my problem after viewing most of the SO question.

There is a Person object, and it has the Fields Name, Age, Rank

I have a MutableArray which will save the data from NSUserDefaults. then it will see if the NSMutableArray is contains that particular object. if not it will add it to NSUserDefaults.

There is some problem when i am adding the person object to NSUserDefaults (I am adding the person object through an array, see code).

When i print [data count] it is always 0. So it might not be getting added to NSUserDefaults properly. Or i might be doing some mistake.

NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];

            if (userDefaults ) {

                NSArray *arr= [userDefaults objectForKey:@"person"];

                data = [NSMutableArray arrayWithArray:arr];

NSLog (@"%i ", [data count]);

                if (! [data containsObject:self.person] ) {

                    [data addObject:self.person];

                    NSArray *personarr= [NSArray arrayWithArray:data];

                    [userDefaults setObject:personarr forKey:@"person"];

                    [userDefaults synchronize];
shajem
  • 2,111
  • 5
  • 22
  • 30

4 Answers4

2

If self.person is an instance of the class Person, you can't put it in User Defaults, even inside an array. From the NSUserDefaults documentation:

The value parameter can be only property list objects: NSData, NSString, NSNumber, NSDate, NSArray, or NSDictionary. For NSArray and NSDictionary objects, their contents must be property list objects.

They suggest this as further reading, which lists the types that count as property list objects.

Amy Worrall
  • 16,250
  • 3
  • 42
  • 65
1

You could implement methods in your Person class to export its data into a Dictionary, and an init method to reconstruct itself from that same Dictionary. As Amorya points out, you cannot stick any arbitrary object into NSUserDefaults.

picciano
  • 22,341
  • 9
  • 69
  • 82
  • I have edited my question. I am not adding a person object to NSUserDefaults, but an array – shajem Feb 15 '12 at 17:26
  • But your "data" array still contain a Person object. This won't work. You have to convert to the valid objects, NSString, NSArray, etc. not just bury the object in an array. – picciano Feb 15 '12 at 17:33
  • Create a method in your model that returns a dictionary filled with all the properties. Then you'll need to create a method to reconstruct it. Or you can just archive it with `NSKeyedArchiver`. – edc1591 Feb 15 '12 at 17:42
0

You are saving your Person object in an NSArray, and then the NSArray to NSUserDefaults, but that is NOT enough. You have to Serialize your Person object before you save it into NSUserDefaults.

This post will explain this better I think: Storing custom objects in an NSMutableArray in NSUserDefaults

Try to NSLog(@"%@" self.person); before your if statement, because I suspect that it could be nil.

I hope that helped you.

Community
  • 1
  • 1
0

If you get an array from NSUserDefaults, it will never be the same pointer, as any current object (like self.person).. so this won't ever work. Comparing objects with == compares their memory adresses.

If you want to compare them, use their contents. (eg. [[person objectForKey: @"Name"] isEqualToString: [person2 objectForKey: @"Name"]]). So you should do it in a loop and check, if the name of the new person is already existing.

Also, arrayWithArray is NOT a initializer of NSMutableArray. So if it should be mutable, do it like that:

NSArray *arr= [userDefaults objectForKey:@"person"];
data = [[arr mutableCopy] autorelease];

And if your count is always zero, check the actual array contents and look whats inside. E.g. like this:

NSArray *arr= [userDefaults objectForKey:@"person"];
CFShow(arr);
calimarkus
  • 9,955
  • 2
  • 28
  • 48