)
我想NSObject
encoded
之前没有NSUserDefaults
正确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);
。
这是我的代码:
Favorite artists before encode: (
"<SBQArtistModel: 0x15671d30>"
)
编码前控制台中的输出是:
Favorite artists after encode: (
)
编码后控制台中的输出:
NSObject
为什么没有保存?
我真的很困惑。代码似乎是正确的,我不明白发生了什么。
由于
修改
我正在我的NSCoding
课程中实施initWithCoder:
协议,并在encodeWithCoder:
.m。
@interface SBQAllArtistsModel : NSObject <NSCoding>
方法>
.h中的代码:
- (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];
}
.m中的代码:
{{1}}
答案 0 :(得分:2)
我得到了解决方案。
我有两个NSObject,一个可以保存我的所有艺术家,每个艺术家一个。因此,在我保存艺术家信息的NSObject中,我有一个BOOL值,如果我选择艺术家作为收藏,我会存储该值。我没有将这个BOOL值添加到他的NSCoding协议方法中,因为BOOL不是一个对象,我不能,所以我创建了一个NSNumber来存储收藏夹的值,并在我添加它之后对于NSCoding协议方法,工作得很好。
我的艺术家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;
}
谢谢,我希望已经解释得很好;)
答案 1 :(得分:1)
对数组进行编码(例如self.artists
)时,数组中的每个对象也会被编码。因此,他们还需要符合< NSCoding >
并实施适当的方法来存档和恢复其内容。
因为你从unarchive但是一个空数组中获得了一个有效的对象,它表明你的对象存档和恢复很好,但是数组的内容没有正确实现协议,所以它们的值会丢失。因此,当您运行谓词时,您将得不到任何结果。
您可以在对象恢复后通过调试self.artists
的内容来验证这一点。