开发者

When saving something to disk with NSKeyedArchiver, where is that stored on the iPhone?

Example: If I used this, where does the iPhone store the file?

if (![NSKeyedArchiver archiveRootObject:your_object toFile:@"filename.plist"])
开发者_运维知识库   // save failed.

On my mac the filename.plist goes to Macintosh HD directly. No folder. Also, the NSKeyedArchiver doesn't seem to ask for a path, just for a file name. Strange or not?

And how's that file backed up with iTunes?


Well that actually depends on where you ask it to save the file, in your case, you're telling it to store filename.plist, which will be stored in your root / .

if you want to store it in a specific location , just give it the full path, something like what Diederik Hoogenboom told you (this will store it in the Documents dir)

OR just give it the absolute path you want to save the file to.

NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *filePath = [documentsDirectory stringByAppendingPathComponent: @"myFile.plist"];
[NSKeyedArchiver archiveRootObject:your_object toFile:filePath];

This should probably have cleared it up a little for you.


archiveRootObject:toFile: (has been deprecated). So you should pass it a path instead of just a filename. Otherwise NSKeyedArchiver will assume you want to store it in the root of the devices hard disk.


Update:

With the data result generated with NSKeyedArchiver, use the Data.write(to:options:) instance method which writes the data object's bytes to the location specified by a given URL.


The proper location to store the files is the documents folder:

NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜