简体   繁体   中英

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:

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 *data = [NSPropertyListSerialization 
                       dataFromPropertyList:root
                                     format:NSPropertyListXMLFormat_v1_0
                           errorDescription:nil];

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

NSLog(@"%@", string);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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