NSKeyedArchiver archivedDataWithRootObject:
Asked Answered
S

2

6

Is the parameter for NSKeyedArchiver archivedDataWithRootObject: supposed to be the array I am trying to save, or the array converted into NSData?

Sym answered 19/9, 2010 at 13:25 Comment(0)
S
14

To convert a generic array to an NSData, you need an archiver! If you know how to feed the NSData, you know how to use NSKeyedArchiver. So:

NSArray* array= ... ;
NSData* data=[NSKeyedArchiver archivedDataWithRootObject:array];

Of course all elements in your array needs to implement encodeWithCoder:.

Sarthe answered 19/9, 2010 at 16:42 Comment(3)
For completeness, you use an NSKeyedUnarchiver to do the reverse.Imitative
"all elements in your array needs to implement encodeWithCoder:" <-- this is only needed for custom objects?Septal
all elements in your array need to have an implementation of encodeWithCoder:, either by Apple or by you. You can check if an Apple-supplied object implements it or not by having a look at the official documentation... see if it implements the protocol NSCoder.Sarthe
H
16

Yuji's answer is right. but more accurately, your element of an array have to implement protocol and fillin your own code to methods initWithCoder: and encodeWithCoder: like:

- (id)initWithCoder:(NSCoder *)decoder {
    if (self = [super init]) {
        self.title = [decoder decodeObjectForKey:@"title"];
        self.author = [decoder decodeObjectForKey:@"author"];
        self.published = [decoder decodeBoolForKey:@"published"];
    }
    return self;
}
- (void)encodeWithCoder:(NSCoder *)encoder {
    [encoder encodeObject:title forKey:@"time"];
    [encoder encodeObject:author forKey:@"author"];
    [encoder encodeBool:published forKey:@"published"];
}

then you can use the archiver and unchariver like:

NSData *data = [NSKeyedArchiver archivedDataWithRootObject:notes];
[[NSUserDefaults standardUserDefaults] setObject:data forKey:@"notes"];

NSData *notesData = [[NSUserDefaults standardUserDefaults] objectForKey:@"notes"];
NSArray *notes = [NSKeyedUnarchiver unarchiveObjectWithData:notesData];

For more, you can get reference "Archiving Objective-C Objects with NSCoding".

Hype answered 29/1, 2013 at 8:28 Comment(0)
S
14

To convert a generic array to an NSData, you need an archiver! If you know how to feed the NSData, you know how to use NSKeyedArchiver. So:

NSArray* array= ... ;
NSData* data=[NSKeyedArchiver archivedDataWithRootObject:array];

Of course all elements in your array needs to implement encodeWithCoder:.

Sarthe answered 19/9, 2010 at 16:42 Comment(3)
For completeness, you use an NSKeyedUnarchiver to do the reverse.Imitative
"all elements in your array needs to implement encodeWithCoder:" <-- this is only needed for custom objects?Septal
all elements in your array need to have an implementation of encodeWithCoder:, either by Apple or by you. You can check if an Apple-supplied object implements it or not by having a look at the official documentation... see if it implements the protocol NSCoder.Sarthe

© 2022 - 2024 — McMap. All rights reserved.