Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

NSArray writeToFile fails

Tags:

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; } ) 
like image 620
Chris Avatar asked Jun 09 '10 22:06

Chris


People also ask

What's a difference between NSArray and NSSet?

The main difference is that NSArray is for an ordered collection and NSSet is for an unordered collection. There are several articles out there that talk about the difference in speed between the two, like this one. If you're iterating through an unordered collection, NSSet is great.

Can NSArray contain nil?

arrays can't contain nil.

Is NSArray ordered?

The answer is yes, the order of the elements of an array will be maintained - because an array is an ordered collection of items, just like a string is an ordered sequence of characters...

How do you declare NSArray in Objective C?

In Objective-C, the compiler generates code that makes an underlying call to the init(objects:count:) method. id objects[] = { someObject, @"Hello, World!", @42 }; NSUInteger count = sizeof(objects) / sizeof(id); NSArray *array = [NSArray arrayWithObjects:objects count:count];


1 Answers

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.

like image 157
warrenm Avatar answered Sep 29 '22 09:09

warrenm