保存自定义对象的NSArray

时间:2012-05-02 13:49:22

标签: objective-c ios inheritance nskeyedarchiver nscoding

我已经创建了UIImage(UIImageExtra)的子类,因为我想要包含额外的属性和方法。

我有一个包含此自定义类的实例的数组。但是,当我保存数组时,看起来UIImageExtra类中的额外数据未保存。

UIImageExtra符合NSCoding,但是没有调用initWithCoder或encodeWithCoder,因为我添加的NSLog语句不会被打印。

我保存数组的方法如下:

- (void)saveIllustrations {
if (_illustrations == nil) {
    NSLog(@"Nil array");
    return;
}

[self createDataPath];
//Serialize the data and write to disk
NSString *illustrationsArrayPath = [_docPath stringByAppendingPathComponent:kIllustrationsFile];
NSMutableData *data = [[NSMutableData alloc] init];
NSKeyedArchiver *archiver = [[NSKeyedArchiver alloc] initForWritingWithMutableData:data];
[archiver encodeObject:_illustrations forKey:kIllustrationDataKey];
[archiver finishEncoding];
[data writeToFile:illustrationsArrayPath atomically: YES];
}

UIImageExtra具有以下用于保存的委托方法:

    #pragma mark - NSCoding

- (void)encodeWithCoder:(NSCoder *)aCoder {
    NSLog(@"Encoding origin data!");
    [super encodeWithCoder:aCoder];
    [aCoder encodeObject:originData forKey:kOriginData];
}

- (id)initWithCoder:(NSCoder *)aDecoder {
    if (self = [super initWithCoder:(NSCoder *) aDecoder]) {
        NSLog(@"Decoding origin data");
        self.originData = [aDecoder decodeObjectForKey:kOriginData];
    }
    return self;
}

我首先创建数组的代码看起来像这样(如果提供任何线索)

        for (NSDictionary *illustrationDict in illustrationDicts) {
        NSString *illustrationString = [illustrationDict objectForKey:@"Filename"];
        NSNumber *xCoord = [illustrationDict objectForKey:@"xCoord"];
        NSNumber *yCoord = [illustrationDict objectForKey:@"yCoord"];
        UIImageExtra *illustration = (UIImageExtra *)[UIImage imageNamed:illustrationString];

        //Scale the illustration to size it for different devices
        UIImageExtra *scaledIllustration = [illustration adjustForResolution];
        NSValue *originData = [NSValue valueWithCGPoint:CGPointMake([xCoord intValue], [yCoord intValue])];
        [scaledIllustration setOriginData:originData];
        [self.illustrations addObject:scaledIllustration];
    }

或者我只是想以错误的方式保存这些数据?非常感谢。

2 个答案:

答案 0 :(得分:3)

初始化数组的代码实际上并没有创建UIImageExtra子类的实例。

UIImageExtra *illustration = (UIImageExtra *)[UIImage imageNamed:illustrationString];

返回一个UIImage。施放它并不能达到您的目的。

UIImageExtra *scaledIllustration = [illustration adjustForResolution];

只是一个UIImage。

一种直接但又冗长的方法是在UIImage周围使UIImageExtra成为包装器。包装器将有一个用于从UIImage初始化的类方法:

+ (UIImageExtra)imageExtraWithUIImage:(UIImage *)image;

然后,您要调用的每个UIImage方法都必须转发到包装的UIImage实例 - 还要小心重新包装例如-adjustForResolution以免再次出现一个未打开的UIImage实例。

更加Objective-C复杂的方法是在UIImage上的 Category 中添加您想要的功能,然后使用方法调配将NSCoding方法替换为您的类别实施。这个棘手的部分(除了必需的Objective-C运行时体操)是存储“额外”数据的地方,因为您无法在类别中添加实例变量。 [标准答案是有一个由UIImage实例的某个合适表示键入的后备字典(如包含其指针值的NSValue),但正如您可以想象的那样,簿记可以快速复杂化。]

退一步,我对新的Cocoa程序员的建议是:“想想一个更简单的方法。如果你想要做的事情很复杂,那就试试别的了。”例如,编写一个简单的ImageValue类,它具有-image方法和-extraInfo方法(并实现NSCoding等),并在您的数组中存储该实例。

答案 1 :(得分:1)

初始化后,无法将对象添加到NSArray。使用NSMutableArray,这可能是问题。