writeToFile working in sim but not on device

蹲街弑〆低调 提交于 2019-12-11 11:53:32

问题


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];

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.

Thanks.


回答1:


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.

You can get your Documents directory with:

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


来源:https://stackoverflow.com/questions/6005544/writetofile-working-in-sim-but-not-on-device

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!