1

I'm trying to write an NSDictionary with a complex structure to a plist for use in an iPhone app. However, the file doesn't seem to write at all, and I have no idea why.

This is what the structure should look like:

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

And this is what my code looks like:

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

I've tried converting the mutables to their immutable counterparts, but that didn't work. I'm able to access data from the final NSDictionary (levels) just fine.

Edit: I should add that this plist is not for actual use in the iphone game. It's for personal use when designing levels. The code won't exist in the final version.

Almo
  • 15,538
  • 13
  • 67
  • 95
Alex Shoup
  • 11
  • 2
  • 4
    I'm thinking that `test.plist` is outside of your sandbox. – Richard J. Ross III Jul 20 '12 at 17:56
  • Where do I put it, then? It's currently under Projectfiles/Resources in my xcode project. – Alex Shoup Jul 20 '12 at 18:00
  • [relevant](http://stackoverflow.com/questions/2502193/writing-nsdictionary-to-plist-in-my-app-bundle) – Dima Jul 20 '12 at 18:04
  • possible duplicate of [Writing a plist](http://stackoverflow.com/questions/5308958/writing-a-plist) – jlehr Jul 20 '12 at 18:12
  • You can read it from resources at runtime, but you can not write to it (app bundle can't be modified because it is signed). You must write to some directory in either /Documents, /Library, /Cache, etc... Mutables are OK. – Nicolas Miari Jul 20 '12 at 19:27
  • Silent failures can also happen when your dictionary contains keys of types that are considered invalid in a plist. E.g. you can have an `NSNumber` as key in an `NSDictionary` but if you try to write that into a file, it will fail. Not sure if this is your case but it might help. – Lvsti Jul 21 '12 at 21:29

2 Answers2

1

A common problem is, that the dictionary isnt valid for string transformation like invalid UTF-8 characters. Make a output on the console to check it.

If that doesnt help you, save only some elements to see what is going on.

Karsten
  • 1,869
  • 22
  • 38
-1

I just posted an answer for this same issue yesterday. Instead of trying to save out the individual data elements of a class save the entire object using NSKeyArchiver and NSCoding. Click the link for a full explanation.

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

Community
  • 1
  • 1
Caranicas
  • 149
  • 1
  • 11