NSCoding with Nested Custom Objects?

前端 未结 2 1892
佛祖请我去吃肉
佛祖请我去吃肉 2021-01-11 14:57

I have a series of nested objects that I am needing to put through the NSCoding protocol so that I can save the top level object into NSUserDefaults.

Here is the st

2条回答
  •  情书的邮戳
    2021-01-11 15:01

    NSCoding isn't magic so it will work 'recursively' if your implementation of the encoding and decoding methods tells it to be.

    Implement the NSCoding methods and pass the data to be encoded to the encoder. Implement the NSCoding methods in all of your custom classes so that when you encode the array all of the contents can be processed appropriately.

    Be sure to call super if the classes superclass also implements NSCoding.

    e.g.

    - (void)encodeWithCoder:(NSCoder *)encoder {
        [encoder encodeObject:self.arrayOfClasses forKey:@"arrayOfClasses"];
    }
    
    - (id)initWithCoder:(NSCoder *)decoder {
        self.arrayOfClasses = [decoder decodeObjectForKey:@"arrayOfClasses"];
    }
    

提交回复
热议问题