Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
402 views
in Technique[技术] by (71.8m points)

iphone - NSArray writeToFile fails

I am trying to save an array, which has some dictionaries inside, to a plist file but it fails. I don't get any errors. I do exactly the same few lines above in the code just with another array and that works.. I can't figure out why it does not save the file.

This is where I save the file: (see some debugger output below)

// When built parse through dictionary and save to file
    for ( NSString *keys in [dicByCountry allKeys] )
    {
        NSArray *arrr = [[NSArray alloc] initWithArray:[dicByCountry objectForKey:keys]];
        NSString *fname = [self filePath:[NSString stringWithFormat:@"regions.cid%@.plist",keys]];
        if (![arrr writeToFile:fname atomically:YES])
            NSLog(@"Could not write file regions.cid%@.plist",keys);
    }

Here some GDB Output

(gdb) po fname
/Users/chris/Library/Application Support/iPhone Simulator/4.0/Applications/44A9FF9E-5715-4BF0-9BE2-525883281420/Documents/regions.cid0.plist


(gdb) po arrr
<__NSArrayI 0x8022b30>(
{
    countryID = "<null>";
    region = "?U00e2vora";
    regionID = 16;
},
{
    countryID = "<null>";
    region = Vicenza;
    regionID = 14;
},
{
    countryID = "<null>";
    region = Wales;
    regionID = 23;
}
)
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

If you read the documentation closely, writeToFile:atomically: expects the array to contain only objects which can be written into a plist file.

Only objects of type:

  • NSString
  • NSData
  • NSDate
  • NSNumber
  • NSArray
  • NSDictionary

are permitted. If you have arrays or dictionaries within the array you're saving, their values will be examined by the same criteria.

This is somewhat more restrictive than what's usually allowed in NSArrays. In particular, the value [NSNull null] is not acceptable.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...