0

变量:

NSMutableDictionary *rootObj;
NSDictionary *innerDict;
NSString *name;
NSArray *scores;

代码:

rootObj = [NSMutableDictionary dictionaryWithCapacity:2];

    scores = [NSArray arrayWithObjects:[NSNumber numberWithInt:6],
              [NSNumber numberWithFloat:4.6], [NSNumber numberWithLong:6.0000034], nil];
    name = @"George Washington";



    innerDict = [NSDictionary dictionaryWithObjects:
                 [NSArray arrayWithObjects: name, scores, nil]
                                            forKeys:[NSArray arrayWithObjects:@"Name", @"Scores", nil]];
    [rootObj setObject:innerDict forKey:@"Washington"];

    scores = [NSArray arrayWithObjects:[NSNumber numberWithInt:8],
              [NSNumber numberWithFloat:4.9],
              [NSNumber numberWithLong:9.003433], nil];
    name = @"Abraham Lincoln";


    innerDict = [NSDictionary dictionaryWithObjects:
                 [NSArray arrayWithObjects: name, scores, nil]
                                            forKeys:[NSArray arrayWithObjects:@"Name", @"Scores", nil]];
    [rootObj setObject:innerDict forKey:@"Lincoln"];

    id plist = [NSPropertyListSerialization dataFromPropertyList:(id)rootObj
                                                          format:NSPropertyListXMLFormat_v1_0 errorDescription:nil];

    // SERIALIZATION

    NSString *path = [[NSBundle mainBundle] pathForResource:@"Data" ofType:@"plist"];
    NSData *xmlData;
    NSString *error;

    xmlData = [NSPropertyListSerialization dataFromPropertyList:plist
                                                         format:NSPropertyListXMLFormat_v1_0
                                               errorDescription:&error];
    if(xmlData) {
        NSLog(@"No error creating XML data.");
        [xmlData writeToFile:path atomically:YES];
        NSFileManager *manger = [[NSFileManager alloc] init];
        NSLog(@"%@", [manger contentsAtPath:path]);
    }
    else {
        NSLog(@"error");
       // [error release];
    }

    NSMutableDictionary *myDictionary = [[NSMutableDictionary alloc] initWithContentsOfFile:path];
    NSLog(@"%@", myDictionary);

输出是:

2013-01-18 14:33:50.066 PListTrials[11281:c07] No error creating XML data.
2013-01-18 14:33:50.067 PListTrials[11281:c07] (null)
2013-01-18 14:33:50.067 PListTrials[11281:c07] (null)

myDictionary 如何为空?代码来自:https ://developer.apple.com/library/mac/#documentation/Cocoa/Conceptual/PropertyLists/CreatePropListProgram/CreatePropListProgram.html#//apple_ref/doc/uid/10000048i-CH5-SW1

4

2 回答 2

1

您不能在应用程序主包上​​编写。在文档包中执行

最初,路径中不存在您的 plist 文件“Data.plist”,因此您需要创建它。最好在要写入数据时检查文件是否存在。尝试以下操作:

NSFileManager *fileManager = [NSFileManager defaultManager];
NSString *path = [[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0] stringByAppendingPathComponent:@"Data.plist"];
if ([fileManager fileExistsAtPath:path] == NO) {
    [fileManager createFileAtPath:path contents:YourContent attributes:nil];
}else {
    [Content writeToFile:path atomically:YES encoding:NSUTF8StringEncoding error:nil];
}
于 2013-01-18T19:48:55.943 回答
0

很简单:您不能写入应用程序包。更改path为可写的内容(例如 Documents 目录)。

更新:

还,

xmlData = [NSPropertyListSerialization dataFromPropertyList:plist
                                                     format:NSPropertyListXMLFormat_v1_0
                                           errorDescription:&error];

是多余的。plist已经是 XML 数据了。

于 2013-01-18T19:44:42.687 回答