我有一个 NSMutableArray,里面有一堆点(这是我定义的一个类)。我想序列化这个 NSMutableArray 以保存到我的磁盘。如果我在我的类 Point 中实现 NSCoding 协议(protocol),是否可以直接在我的 NSMutableArray 中使用 NSKeyedArchiver?
就像这个例子:
NSMutableData *data = [NSMutableData new];
NSKeyedArchiver *archiver = [[NSKeyedArchiver alloc] initForWritingWithMutableData:data];
[archiver encodeObject:myArray forKey:@"array"];
[data writeToFile:filename attomically:YES];
[archiver release];
[data release];
最佳答案
是的,这是可能的,引用 Apple documentation :
The NSArray class adopts the NSCopying, NSMutableCopying, and NSCoding protocols;
请注意,如果您正在使用点,也许您应该使用 Apple CGPoint
结构,它带有许多方便的实用程序:CGPointFromString
和 NSStringFromCGPoint
see Apple documentation for more information。
还有这些你可能感兴趣的CGPointEqualToPoint
, CGRectContainsPoint
, CGRectIntersectsRect
or CGRectContainsRect
... see Apple documentation for more information
关于iphone - 序列化一个包含对象的 NSMutableArray,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5515621/