Question

)

My NSObject is not being encoded correctly before I want to save it with NSUserDefaults.

This is my code:

NSUserDefaults *userDefault=[NSUserDefaults standardUserDefaults];

NSLog(@"Favorite artists before encode: %@", self.model.favoriteArtists);

NSData *myEncodedObject = [NSKeyedArchiver archivedDataWithRootObject:self.model];
                        [userDefault setObject:myEncodedObject forKey:[NSString stringWithFormat:@"Favorites"]];

[userDefault synchronize];


NSData *myDecodedObject = [userDefault objectForKey: [NSString stringWithFormat:@"Favorites"]];

self.model=[NSKeyedUnarchiver unarchiveObjectWithData: myDecodedObject];

NSLog(@"Favorite artists after encode: %@",self.model.favoriteArtists); 

The output in console before encode is this:

Favorite artists before encode: (
    "<SBQArtistModel: 0x15671d30>"
)

The output in console after encode:

Favorite artists after encode: (
)

Why is nothing saved?

I'm really stuck with this. The code seems correct and I don't understand whats happening.

Thanks

EDIT

I'm implementing in my NSObject class the NSCoding protocol and implementing the initWithCoder: and encodeWithCoder: methods on the NSObject .m.

Code in .h:

@interface SBQAllArtistsModel : NSObject <NSCoding>

Code in .m:

- (void)encodeWithCoder:(NSCoder *)encoder
{

    //Encode properties, other class variables, etc
    [encoder encodeObject:self.artists forKey:@"artists"];
    [encoder encodeObject:self.items forKey:@"items"];
    [encoder encodeObject:self.arrays forKey:@"arrays"];
    [encoder encodeObject:self.artistFav forKey:@"artistFav"];

}
- (id)initWithCoder:(NSCoder *)decoder
{
    self = [super init];
    if( self != nil )
    {
        self.artists = [decoder decodeObjectForKey:@"artists"];
        self.items = [decoder decodeObjectForKey:@"items"];
        self.arrays = [decoder decodeObjectForKey:@"arrays"];
        self.artistFav = [decoder decodeObjectForKey:@"artistFav"];

    }
    return self;
}

-(NSArray *)favoriteArtists{
    NSPredicate *predicate = [NSPredicate predicateWithFormat: @"Favorite == YES"];
    return [self.artists filteredArrayUsingPredicate: predicate];
}
Was it helpful?

Solution 2

When you encode an array (such as self.artists), each of the objects in the array are also encoded. So they need to also conform to < NSCoding > and implement the appropriate methods to archive and restore their content.

Because you get a valid object back from your unarchive but an empty array it suggests that your object archive and restore are fine, but that the contents of the array(s) are not properly implementing the protocol so their values are lost. Hence, when you run the predicate you get no results.

You can verify this by debugging the contents of self.artists after the object is restored.

OTHER TIPS

I got the solution.

I have two NSObjects, one that saves all my artists, and one per each artist. So in the NSObject where I save the artist information, I had a BOOL value where I stored the value if I selected the artist as favourite. I hadn't added this BOOL value to his NSCoding protocols methods, because the BOOL isn't a object, and I couldn't, so I have created a NSNumber to store the value of the Favorite on it, and after I added it to the NSCoding protocols methods, is working good.

Code in my Artist NSObject.

.h

@interface SBQArtistModel : NSObject <NSCoding>

@property (strong, nonatomic) NSString *name;
@property (strong, nonatomic) NSString *bio;
@property (strong, nonatomic) NSString *location;
@property (strong, nonatomic) NSString *linkSoundcloud;
@property (strong, nonatomic) NSString *linkFacebook;
@property (strong, nonatomic) NSString *thumbnail;
@property (strong, nonatomic) NSString *image;
@property (strong, nonatomic) NSNumber *numberFavorite;
@property (nonatomic) BOOL Favorite;

.m

- (void)encodeWithCoder:(NSCoder *)encoder
{


    [encoder encodeObject:self.name forKey:@"name"];
    [encoder encodeObject:self.bio forKey:@"bio"];
    [encoder encodeObject:self.location forKey:@"location"];
    [encoder encodeObject:self.linkSoundcloud forKey:@"linkSoundcloud"];
    [encoder encodeObject:self.linkFacebook forKey:@"linkFacebook"];
    [encoder encodeObject:self.thumbnail forKey:@"thumbnail"];
    [encoder encodeObject:self.image forKey:@"image"];
    [encoder encodeObject:self.numberFavorite forKey:@"numberFavorite"];

}

- (id)initWithCoder:(NSCoder *)decoder
{
    self = [super init];
    if( self != nil )
    {
        self.name = [decoder decodeObjectForKey:@"name"];
        self.bio = [decoder decodeObjectForKey:@"bio"];
        self.location = [decoder decodeObjectForKey:@"location"];
        self.linkSoundcloud = [decoder decodeObjectForKey:@"linkSoundcloud"];
        self.linkFacebook = [decoder decodeObjectForKey:@"linkFacebook"];
        self.thumbnail = [decoder decodeObjectForKey:@"thumbnail"];
        self.image = [decoder decodeObjectForKey:@"image"];
        self.numberFavorite = [decoder decodeObjectForKey:@"numberFavorite"];

    }
    return self;
}

Thanks, and I hope have explained well ;)

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top