0

我有一个正在创建的 plist 文件,但我不想写入文件,而是想将其内容作为字符串打印到 NSLog:

NSMutableDictionary* root = [NSMutableDictionary dictionaryWithCapacity:1];
NSMutableArray* puzzle = [NSMutableArray arrayWithCapacity: [self.puzzlePieces count] ];
root[@"puzzle"] = puzzle;

[self.puzzlePieces enumerateObjectsUsingBlock:^(KTPuzzlePiece* piece, NSUInteger idx, BOOL *stop) {
    NSMutableDictionary* pieceInfo = [piece.pieceInfo mutableCopy];
    pieceInfo[@"x"] = [NSNumber numberWithFloat: piece.position.x];
    pieceInfo[@"y"] = [NSNumber numberWithFloat: piece.position.y];

    [puzzle addObject:pieceInfo];
}];


NSData *data = [NSPropertyListSerialization dataFromPropertyList:root
                                                          format:NSPropertyListXMLFormat_v1_0
                                                errorDescription:nil];

但是,以下输出十六进制值

    NSLog(@"Data: %@", data);
4

1 回答 1

2

你只是少了一步。您需要将您的 NSData 转换为一个字符串,您应该很高兴:

NSData *data = [NSPropertyListSerialization 
                       dataFromPropertyList:root
                                     format:NSPropertyListXMLFormat_v1_0
                           errorDescription:nil];

NSString *string = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];

NSLog(@"%@", string);
于 2012-10-31T18:04:18.050 回答