writeToFile在sim中工作,但不在设备上工作 [英] writeToFile working in sim but not on device

查看:83
本文介绍了writeToFile在sim中工作,但不在设备上工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

此代码在模拟器上有效,但在设备上无效。我肯定这与写入文件有关:

This code works on simulator but not on device. I am positive this has something to do with writing to file:

NSString *path = [[NSBundle mainBundle] pathForResource:@"SongData" ofType:@"plist"];
        NSArray *tempData = [[NSArray alloc] initWithContentsOfFile: path];

        NSMutableArray *arr = [[NSMutableArray alloc] init];
        for (NSDictionary *dict in tempData) {
            NSMutableDictionary *new = [[dict mutableCopy] autorelease];
            if ([[new objectForKey:@"Song Title"] isEqualToString:[[tableData objectAtIndex:[indexPath row]] objectForKey:@"Song Title"]]) {
                BOOL fav = [[new objectForKey:@"Favorite"] boolValue];
                [new setObject:[NSNumber numberWithBool:!fav] forKey:@"Favorite"];
            }
            [arr addObject:new];
        }
        [arr writeToFile:path atomically:YES];

我基本上是写NSMutableArray的文件,我不确定原子上是什么意思,但是我已经尝试过是和否,两者都不起作用。

I am basically writing to file the NSMutableArray, I am not sure what atomically means, but I have tried both yes and no, both don't work.

谢谢。

推荐答案

您无法写入应用包,由于沙箱的限制(这也会破坏您的代码签名)。您应该改为写入应用程序的documents目录。

You cannot write into your app bundle, because of sandbox restrictions (it would also break your code signature). You should write to your app's documents directory instead.

您可以使用以下命令获取Documents目录:

You can get your Documents directory with:

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

这篇关于writeToFile在sim中工作,但不在设备上工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆