Encode NSArray or NSDictionary using NSCoder

前端 未结 3 1246
花落未央
花落未央 2020-12-09 10:09

I was wondering whether or not it is possible to use the NSCoder method:

- (void)encodeObject:(id)objv forKey:(NSString *)key

to encode eit

3条回答
  •  不思量自难忘°
    2020-12-09 11:09

    If the array or dictionary is the root object you should do

    NSData * encodedData = [NSKeyedArchiver archivedDataWithRootObject:someArray];
    

    or

    BOOL success = [NSKeyedArchiver archiveRootObject:someArray toFile:filePath];
    

    If it is an instance variable of a custom class, in the -encodeWithCoder: method should do

    [coder encodeObject:someArray forKey:@"someArray"];
    

    and then in the -initWithCoder: method

    someArray = [[coder decodeObjectForKey:@"someArray"] retain];
    

提交回复
热议问题