gpt4 book ai didi

ios - NSObject 在编码后不会与 NSUserDefaults 一起保存

转载 作者:塔克拉玛干 更新时间:2023-11-02 10:06:12 25 4
gpt4 key购买 nike

)

在我想用 NSUserDefaults 保存它之前,我的 NSObject 没有被正确编码

这是我的代码:

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 协议(protocol)并实现了 initWithCoder:encodeWithCoder: NSObject .m.

上的方法

.h 中的代码:

@interface SBQAllArtistsModel : NSObject <NSCoding>

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

最佳答案

我得到了解决方案。

我有两个 NSObject,一个保存我所有的艺术家,每个艺术家一个。因此,在我保存艺术家信息的 NSObject 中,我有一个 BOOL 值,如果我选择艺术家作为收藏夹,我会在其中存储该值。我没有将这个 BOOL 值添加到他的 NSCoding 协议(protocol)方法中,因为 BOOL 不是一个对象,我不能,所以我创建了一个 NSNumber 来存储 Favorite 的值,在我添加它之后NSCoding 协议(protocol)方法,运行良好。

我的 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;
}

谢谢,我希望解释得很好 ;)

关于ios - NSObject 在编码后不会与 NSUserDefaults 一起保存,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22225940/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com