我有一個自定義類符合NSCoding協議,但仍然拒絕編碼,當我調用寫入文件。自定義目標c類不編碼
@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;
}
正如您所看到的,變量映射是一個包含字符串的多維數組。不知道這是不是搞砸了什麼。
我順便說一句接口
@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;
什麼對象在調用'writeToFile:'on?它與你的PXLevel類有什麼關係? –
具體而言,PXLevel對象的可變數組(上面的實現)。我檢查了代碼,該方法可以保存一個空數組到一個新的plist,但添加一個PXLevel到它並沒有任何反應。 – evilseto