0

NSDictionary (+dictionaryWitContentsOfFile:) を作成し、いくつかの値を -setValue:ForKey: で変更し、アプリケーションが終了したら、この辞書を -writeToFile: で自動的にファイルに書き戻します。別の user-account内で同じプログラムを開くまで、すべてがうまく機能します。

このエラーの原因は何ですか?

ドキュメントには、 -writeToFile:automatically: 値の一部が plist に適していない場合に NO が返されると書かれています。これは間違いなくここでの問題ではありません。今のところ言えることは、開発元のアカウントにいるときにプログラムがファイルを書き込むことができるということだけです。私の Mac には管理者ではない別のユーザー アカウントがあり、そこに -writeToFile: 自動的に NO が返されます (これはコンソールに表示されます)。そのアカウントを管理者にしましたが、プログラムはまだそこに保存できません。

助けてくれてありがとう、

ヴォルフガング

PS: 冗長でない情報をこれ以上提供することはできないと思います。あなたの手にもっと欲しい場合は、ここにコードがあります:

- (void)awakeFromNib
{
    NSString *path = [NSString stringWithFormat:@"%@/%@.plist",[[NSBundle mainBundle] resourcePath],NSUserName()];
    if (![[NSFileManager defaultManager] fileExistsAtPath:path])
    {
        NSLog(@"create new plist");
        path = [[NSBundle mainBundle] pathForResource:@"DefaultAppData" ofType:@"plist"];
    }
    NSLog(@"open path:%@", path);
    myPlist = [NSDictionary dictionaryWithContentsOfFile:path];
}

-(void)changeSomething
{
    [myPlist setValue:[NSNumber numberWithBool:NO]] forKey:@"aKey"];
}


-(void)applicationWillTerminate:(NSNotification*)notification
{
    NSLog(@"dict: %@",myPlist);
    NSString *path = [NSString stringWithFormat:@"%@/%@.plist",[[NSBundle mainBundle] resourcePath],NSUserName()];
    NSLog(@"save at path:%@ successful:%d", path, [myPlist writeToFile:path atomically:YES]);
}
4

1 に答える 1

0

わかりました。ほとんどの Mac ユーザーはアプリケーション フォルダーに書き込む権限を持っていないため、アプリのリソース フォルダーは、何かを保存するのに適切な場所ではありません。

今使ってる

NSString *path = [NSString stringWithFormat:@"%@/Library/Preferences/com.myCompany.myApp.plist",NSHomeDirectory()];

代わりは。

于 2011-08-27T11:23:18.963 に答える