2012-10-31 56 views
0

我有我創建一個plist文件,但不是寫入文件,我想打印出它的內容作爲一個字符串的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]; 

但是,以下低於輸出十六進制值

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

NSLog(@「Data:%@」,root); – NeverBe

+0

這不會輸出XML字符串,例如在純文本編輯器中打開plist時發現的XML字符串。 – 1dayitwillmake

回答

2

您只是缺少一個步驟。你需要將你的NSData轉換爲一個字符串,你應該很好走:

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

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

NSLog(@"%@", string);