将包含另一个自定义对象的自定义对象保存到 NSUserDefaults 中

发布于 2024-12-12 18:04:49 字数 141 浏览 0 评论 0原文

我了解如何在 NSUser Defaults 中存储自定义对象,但是当我尝试实现保存其中包含不同类型对象的对象时(如果我没有记错的话,它称为组合),请遵循与我相同的内部对象步骤对于第一个,我遇到了运行时错误。您能否详细描述一下我必须采取的步骤才能正确保存和检索所有内容

I understand how to store a custom object in NSUser Defaults but when i tried to implement saving an object with an object of different type inside it (it is called composition if i'm not mistaken) following the same steps for inner object as i did for the first one i got runtime error. Could you please minutely describe steps that i have to undertake in order to save and retrieve everything correctly

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

情归归情 2024-12-19 18:04:49

你的所有对象都应该实现 NSCoding 协议。 NSCoding 对要保存的对象递归地工作。例如,您有 2 个自定义类

@interface MyClass : NSObject {

}
@property(nonatomic, retain) NSString *myString;
@property(nonatomic, retain) MyAnotherClass *myAnotherClass;

@interface MyAnotherClass : NSObject {

}
@property(nonatomic, retain) NSNumber *myNumber;

要将 MyClass 对象保存到 NSUserDefaults,您需要对这两个类实现 NSCoding 协议:

对于第一个类:

-(void)encodeWithCoder:(NSCoder *)encoder{
    [encoder encodeObject:self.myString forKey:@"myString"];
    [encoder encodeObject:self.myAnotherClass forKey:@"myAnotherClass"];
}

-(id)initWithCoder:(NSCoder *)decoder{
    self = [super init];
    if ( self != nil ) {
        self.myString = [decoder decodeObjectForKey:@"myString"];
        self.myAnotherClass = [decoder decodeObjectForKey:@"myAnotherClass"];
    }
    return self;
}

对于第二个类:

-(void)encodeWithCoder:(NSCoder *)encoder{
    [encoder encodeObject:self.myNumber forKey:@"myNumber"];
}

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

注意,如果您的另一个类(上面的 MyAnotherClass)也有自定义对象 那么自定义对象也应该实现 NSCoding。即使你有隐式包含自定义对象的 NSArray,你也应该为这些对象实现 NSCoding。

All your objects should implement NSCoding protocol. NSCoding works recursively for objects that would be saved. For example, you have 2 custom classes

@interface MyClass : NSObject {

}
@property(nonatomic, retain) NSString *myString;
@property(nonatomic, retain) MyAnotherClass *myAnotherClass;

@interface MyAnotherClass : NSObject {

}
@property(nonatomic, retain) NSNumber *myNumber;

For saving MyClass object to NSUserDefaults you need to implement NSCoding protocol to both these classes:

For first class:

-(void)encodeWithCoder:(NSCoder *)encoder{
    [encoder encodeObject:self.myString forKey:@"myString"];
    [encoder encodeObject:self.myAnotherClass forKey:@"myAnotherClass"];
}

-(id)initWithCoder:(NSCoder *)decoder{
    self = [super init];
    if ( self != nil ) {
        self.myString = [decoder decodeObjectForKey:@"myString"];
        self.myAnotherClass = [decoder decodeObjectForKey:@"myAnotherClass"];
    }
    return self;
}

For second class:

-(void)encodeWithCoder:(NSCoder *)encoder{
    [encoder encodeObject:self.myNumber forKey:@"myNumber"];
}

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

Note, if your another class (MyAnotherClass above) has also custom object then that custom object should implement NSCoding as well. Even you have NSArray which implicity contains custom objects you should implement NSCoding for these objects.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文