NSDictionary不会写入plist

问题描述:

我想写一个复杂结构的NSDictionary给plist用于iPhone应用程序。但是,该文件似乎根本不写,我不知道为什么。NSDictionary不会写入plist

这是结构应该是什么样子:

Level    Dict 
    Roads   Array 
    Road1   Array 
     Vertex1  Dict 
     x   Number 
     y   Number 
     Vertex2  Dict 
     ... 

这是我的代码如下所示:

NSMutableDictionary *levels = [[NSMutableDictionary alloc] init]; 
NSMutableArray *roads = [[NSMutableArray alloc] init]; 
for(ChainLinkRoad *r in data.roads){ 
    NSMutableArray *road = [[NSMutableArray alloc] init]; 
    for(ChainPoint *p in r.v){ 
     NSNumber *x = [NSNumber numberWithFloat: p.x]; 
     NSNumber *y = [NSNumber numberWithFloat: p.y]; 
     NSMutableDictionary *vertex = [[NSMutableDictionary alloc] init]; 
     [vertex setObject:x forKey:@"x"]; 
     [vertex setObject:y forKey:@"y"]; 
     [road addObject:vertex]; 
    } 
    [roads addObject:road]; 
} 
[levels setObject:roads forKey:@"Roads"]; 
bool b = [levels writeToFile:@"test.plist" atomically:YES]; 
//returns true 

我已经试过转换mutables他们不可变,但没有工作。我能够从最终的NSDictionary(级别)访问数据就好。

编辑:我应该补充说,这个plist不是在iphone游戏中实际使用的。在设计关卡时用于个人使用。代码将不会在最终版本中存在。

+4

我在想'test.plist'在你的沙箱外。 – 2012-07-20 17:56:08

+0

那我把它放在哪里呢?它目前在我的xcode项目中的Projectfiles/Resources下。 – 2012-07-20 18:00:30

+0

[relevant](http://*.com/questions/2502193/writing-nsdictionary-to-plist-in-my-app-bundle) – Dima 2012-07-20 18:04:44

我昨天刚发布了同样的问题的答案。而不是试图保存一个类的单个数据元素,使用NSKeyArchiver和NSCoding保存整个对象。点击链接获取完整的解释。

How can I save Array of Class Objects in to a Plist(Iphone Development)

+1

这不是我问的问题,但它肯定会对我的项目有所帮助!谢谢! – 2012-07-20 21:32:22

一个常见的问题是,在字典心不是有效的串变换像无效的UTF-8字符。在控制台上输出以检查它。

如果这不帮助你,只保存一些元素,看看发生了什么。