我有一个符合 NSCoding 协议的自定义类,但当我调用 write to file 时仍然拒绝编码。
@implementation PXLevel
- (id)initWithName:(NSString *)name andArray:(NSMutableArray *)map {
self = [super init];
self.name = name;
self.map = map;
return self;
}
- (id)initWithName:(NSString *)name {
self = [super init];
self.name = name;
NSMutableArray *tempArray = [[NSMutableArray alloc] init];
for (int i = 0; i < 8; i++) {
NSMutableArray *line = [[NSMutableArray alloc] init];
for (int j = 0; j < 8; j++) {
[line addObject:@"grass"];
}
[tempArray addObject:line];
}
self.map = tempArray;
return self;
}
-(void) encodeWithCoder:(NSCoder *)aCoder
{
[aCoder encodeObject:self.name forKey:@"levelName"];
[aCoder encodeObject:self.map forKey:@"levelMap"];
}
-(id) initWithCoder:(NSCoder *)aDecoder
{
if((self = [super init]))
{
self.name = [aDecoder decodeObjectForKey:@"levelName"];
self.map = [[aDecoder decodeObjectForKey:@"levelMap"] mutableCopy];
}
return self;
}
如您所见,变量 map 是一个包含字符串的多维数组。不知道这是搞砸了还是什么。
我的界面顺便说一句
@interface PXLevel : NSObject <NSCoding>
@property (nonatomic, strong) NSString *name;
@property (nonatomic, strong) NSMutableArray *map;
- (id)initWithName:(NSString *)name andArray:(NSMutableArray *)map;
- (id)initWithName:(NSString *)name;