繁体   English   中英

保存自定义对象的NSArray

[英]Saving an NSArray of custom objects

我已经创建了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];
    }

还是我会以错误的方式保存此数据? 非常感谢。

您用来初始化数组的代码实际上并未创建UIImageExtra子类的实例。

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

返回一个UIImage。 投射不符合您的预期。

UIImageExtra *scaledIllustration = [illustration adjustForResolution];

仍然只是一个UIImage。

解决这个问题的一种直接但冗长的方法是使UIImageExtra成为UIImage的包装 包装器将具有用于从UIImage初始化的类方法:

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

然后,要调用的每个UIImage方法都必须转发到包装的UIImage实例-还应小心地重新包装例如-adjustForResolution的结果,以免再次遇到未包装的UIImage实例。

一种更Objective-C复杂的方法是将所需的功能添加到UIImage的“ 类别 ”中,然后使用“ 方法模糊处理”将NSCoding方法替换为您的类别实现。 其中的棘手部分(除了必需的Objective-C运行时体操之外)是用于存储“额外”数据的位置,因为您不能在类别中添加实例变量。 [标准答案是使用由UIImage实例的某些适当表示(例如包含指针值的NSValue)作为键的后备字典,但是您可以想象簿记会很快变得复杂。]

退后一会,我对新的Cocoa程序员的建议是:“想想一种更简单的方法。如果您想做的就是这么复杂,请尝试其他方法。” 例如,编写一个简单的ImageValue类,该类具有-image方法和-extraInfo方法(并实现NSCoding等),并将其实例存储在数组中。

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM