Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

writeToFile fails - how do I debug WHY it fails (what tools)?

Tags:

xcode

ios

iphone

I'm getting an NSDictionary from JSON (using SBJson), and I want to store it. I'm using

[liveData writeToFile:localFilePath atomically:YES]

and it fails. The data looks like its all NSString, NSDictionary, and NSArray (which "atomically:YES" demands). I used the same localFilePath elesewhere.

So my question is: how can I find out where the problem is? What tools can I use to understand why writeToFile fails? The log doesn't show an error message.

like image 824
MM. Avatar asked Sep 22 '11 19:09

MM.


3 Answers

It may have multiple reasons:

  • The path you are writing to is wrong, not writable (you don't have write access to it), or the parent directory does not exists (if localFilePath is "/path/to/file.plist" but the directory "/path/to/" does not exists, it will fail)
  • The liveData dictionary does contains objects that are not PropertyList objects. Only NSData, NSDate, NSNumber, NSString, NSArray, or NSDictionary objects can be written into a Property List file (and writeToFile:atomically: do write to a plist file so the dictionary do have to contains only PList objects for that method to succeed)
like image 95
AliSoftware Avatar answered Nov 14 '22 20:11

AliSoftware


I know this is a 2 year old question. But since I just had this same problem and fixed it here's what I found. I bet your NSDictionary has some keys that are not NSStrings.

Instead of keying like:

[_myDictionay setObject:thisObj forKey:[NSNumber numberWithInt:keyNumber]];

Key like:

[_myDictionay setObject:thisObj forKey:[NSString stringWithFormat:@"%i",numberWithInt:keyNumber]];

That fixed my problem right up.

The top way can't be saved to a plist file.

Since you're getting your info from a JSON conversion, it is possible that there are some objects or keys that are NSNumbers in there. You would have to convert them. But that's a pain.

So since you already have it in json, just store it as the json string in its entirety in a @"data" key and the re-expand it when you load the plist later back into your array or dict.

like image 20
badweasel Avatar answered Nov 14 '22 21:11

badweasel


I've tried saving an NSDictionary to disk with only numbers for keys and values. Switching the keys to NSString works but not when they keys are NSNumber. Do keys have to be NSString?

EDIT: I know better now that it can be any object that responds to hash; often it's NSString's, though.

like image 27
matrinox Avatar answered Nov 14 '22 21:11

matrinox