shajem
shajem

Reputation: 2121

Adding to NSUserDefaults - Beginner

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

Upvotes: 1

Views: 155

Answers (4)

calimarkus
calimarkus

Reputation: 9977

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

Upvotes: 0

Mohamed Ibrahim
Mohamed Ibrahim

Reputation: 190

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.

Upvotes: 0

Amy Worrall
Amy Worrall

Reputation: 16337

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.

Upvotes: 2

picciano
picciano

Reputation: 22701

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.

Upvotes: 1

Related Questions