开发者

Odd behavior from Dictionary to URL writing/reading

开发者 https://www.devze.com 2023-04-03 03:42 出处:网络
I\'ve been looking all over the site for a similar issue, but none of the fixes for any of the other questions I\'ve found have helped, so maybe I\'m looking at it wrong or something. Anyways here is

I've been looking all over the site for a similar issue, but none of the fixes for any of the other questions I've found have helped, so maybe I'm looking at it wrong or something. Anyways here is my code:

NSFileManager *fm = [[NSFileManager alloc] init];
NSError *err = nil;
NSURL *ASD =  [fm URLForDirectory:NSApplicationSupportDirectory inDomain:NSUserDomainMask appropriateForURL:nil create:YES error:&err];

if (!err) {
    NSLog(@"writing file");
    NSURL* path = [NSURL URLWithString:@"license.plist" relativeToURL:ASD];
    [dict writeToURL:path atomically:NO];
    NSString* content = [NSString stringWithContentsOfURL:path encoding:NSUTF8StringEncoding error:&err];
    if (!err) {
        NSLog(@"File:%@",content);
    }
    else
    {
        NSLog(@"Error opening file: %@",err);
    }
    return YES;
}

Given that dict is a valid Dictionary(EDIT: made the incorrect assumption that even if the file was empty it would still init the Dictionary, so added an empty init even if the file is empty), I get the following log messa开发者_如何学Goges:

2011-09-07 15:59:58.883 iActNow[10572:707] writing file 2011-09-07 15:59:58.889 iActNow[10572:707] Error opening file: Error Domain=NSCocoaErrorDomain Code=260 "The operation couldn’t be completed. (Cocoa error 260.)" UserInfo=0x4ba4d0 {NSFilePath=/var/mobile/Applications/6BA775C6-29F6-4362-8860-9ADF84598531/Library/Application Support/license.plist, NSUnderlyingError=0x497080 "The operation couldn’t be completed. No such file or directory"}

So for whatever reason it's not actually writing the file. Any ideas?


I tested your code by replacing this call:

[dict writeToURL:path atomically:NO];  

with this one:

[@"Foo" writeToURL:path atomically:NO];

and file was stored properly.

My bet is that in dict you're holding a custom objects that NSDictionary is not capable of writing to file by definition (i.e. by Apple specs). You'll have to probably use a different way of storing your data to a file - maybe Archives and Serializations Programming Guide can help you?

EDIT: I've just noticed that you named your file "license.plist" - check out section "Reading and Writing Property-List Data" of Property List Programming Guide


URLForDirectory:... inDomain:... only returns the path where the requested standard directory should be.

It doesn't ensure that this directory actually exists.

I guess it's not the case for the ASD. You may need to create this directory (and maybe intermediate directories as well too).


The reson that it is doing this is that NSURLs cant be saved to disk i dont believe, to save a NSURL first you need to change it into a NSString by using:

[SomeUrl absoluteString]

Then save it to disk, when you want to load it, you have to load it into a string then use:

[TheString absoluteURL]

And this goes for NSURLs that are in other objects like NSArrays and NSDictionaries

0

精彩评论

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

关注公众号