WriteToFile fails - how do I debug WHY it doesn't work (what tools)?

I get NSDictionary from JSON (using SBJson) and I want to save it. I use

[liveData writeToFile:localFilePath atomically:YES] 

and he fails. The data looks like all NSString, NSDictionary, and NSArray (which "atomically: YES" requires). I used the same localFilePath elsewhere.

So my question is: how can I find out where the problem is? What tools can I use to understand why writeToFile fails? The error message is not displayed in the log.

+6
source share
3 answers

It may have several reasons:

  • The path that you write is wrong, is not writable (you do not have write permissions), or the parent directory does not exist (if localFilePath have "/path/to/file.plist", but the directory "/ path / to /" does not exist, it does not work)
  • The liveData dictionary contains objects that are not PropertyList objects. Only NSData , NSDate , NSNumber , NSString , NSArray or NSDictionary can be written to the property list file (and writeToFile:atomically: do write to plist, so the do dictionary should contain only PList objects to successfully execute this method)
+7
source

I know this is a 2 year question. But since I had the same problem and I fixed it here that I found. I bet your NSDictionary has a few keys that are not NSStrings.

Instead of typing like:

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

A key like:

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

This fixed my problem.

The top path cannot be saved in the plist file.

Since you are receiving information from a JSON conversion, it is possible that there are some objects or keys that have NSNumbers there. You will have to convert them. But it is a pain.

So, since you already have it in json, just save it as a whole json string in the @ "data" key and redeploy it when you load plist into your array or dict again.

+4
source

I tried to save NSDictionary to disk with only numbers for keys and values. Switching keys to NSString works, but not when they are NSNumber. Do I need NSString keys?

EDIT: now I know that this can be any object that responds to a hash; often this is an NSString, however.

+1
source

Source: https://habr.com/ru/post/897859/


All Articles