简体   繁体   English

将plist文件打印到NSLog

[英]Printing plist file to NSLog

I have a plist file I'm creating, but instead of writing to a file I would like to print out it's contents as a string to NSLog: 我有一个我正在创建的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];

However, the following below outputs hex values 但是,以下输出十六进制值

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

You're just missing one step. 你只是错过了一步。 You need to convert your NSData to a string and you should be good to go: 你需要将NSData转换为字符串,你应该好好去:

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

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

NSLog(@"%@", string);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM