-writeToFile:自动:(NSDictionary)在某些用户帐户上不成功

发布于 2024-12-01 10:41:25 字数 1361 浏览 1 评论 0原文

我创建一个 NSDictionary (+dictionaryWitContentsOfFile:),使用 -setValue:ForKey: 更改一些值,然后,当应用程序终止时,使用 -writeToFile:automatically 将此字典写回到文件中。 一切都运行良好,直到我在另一个用户帐户中打开相同的程序。

有谁预感到什么可能导致此错误?

文档只是说 -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]);
}

I create an NSDictionary (+dictionaryWitContentsOfFile:), change some values with -setValue:ForKey: and then, when the application terminates, write this dictionary back to a file with -writeToFile:automatically.
Everything works great until I open the same program within another user-account.

Has anyone a presentiment what could cause this error?

The documentation just says that -writeToFile:automatically: returns NO when some of its values are not adequate for a plist. This is definitely not the problem here.
All I can say for now is that the program can write the file when I'm at the account I'm developing from. There is another user account on my Mac which is no admin and there -writeToFile:automatically returns NO (I see this in the Console). I made that account admin but the program still fails to save there.

Thank you for any help,

Wolfgang

PS: I think i can't give you more information which isn't redundant. If you want some more in your hands, here is the code:

- (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]);
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

帅气称霸 2024-12-08 10:41:25

好吧,发现了。应用程序的资源文件夹不是保存任何内容的正确位置,因为大多数 Mac 用户没有在应用程序文件夹中写入的权限。

我现在用的

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

是。

Ok, found it out. The app's resources folder is not the right place to save anything, because the most Mac users do not have the right to write in the application folder.

I now use

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

instead.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文