将NSMutableArray转成NSData

        tempArr = [NSMutableArray arrayWithCapacity:9];
                   
        for (int i = 0; i < 9; i++) {
            NSMutableArray *tmA = [NSMutableArray arrayWithCapacity:9];
            for (int j = 9-1; j >= 0; j--) {
                tiles *tile = [[tiles alloc] initCreateTileX:i TileY:j];
                [tmA addObject:tile];
            }
            [tempArr addObject:tmA];
        }
        [tempArr retain];

        NSData *teD = [NSKeyedArchiver archivedDataWithRootObject:tempArr];

        NSMutableArray *teA = [NSKeyedUnarchiver unarchiveObjectWithData:teD];
        int num = [teA count];




另外,转换时,要转换的数据必须是要实现NSCoding持久化

先在接口中实现协议<NSCoding>来支持encode和decode功能

然后再实现这两个方法

#pragma mark NSCoding 
#define kTitleKey       @"Title"
#define kRatingKey      @"Rating" 

- (void) encodeWithCoder:(NSCoder *)encoder {  
      [encoder encodeObject:_title forKey:kTitleKey]; 
      [encoder encodeFloat:_rating forKey:kRatingKey];
} 

- (id)initWithCoder:(NSCoder *)decoder {   

     NSString *title = [decoder decodeObjectForKey:kTitleKey];  
     float rating = [decoder decodeFloatForKey:kRatingKey];  
     return [self initWithTitle:title rating:rating];

}

你可能感兴趣的:(table)