【问题标题】:writeToFile working in sim but not on devicewriteToFile 在 sim 中工作但不在设备上
【发布时间】:2011-05-14 23:50:12
【问题描述】:

此代码适用于模拟器,但不适用于设备。我很肯定这与写入文件有关:

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 文件,我不确定原子意味着什么,但我尝试了是和否,两者都不起作用。

谢谢。

【问题讨论】:

    标签: objective-c cocoa-touch nsmutablearray writetofile


    【解决方案1】:

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

    您可以通过以下方式获取您的 Documents 目录:

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-08-19
      • 2023-03-23
      • 1970-01-01
      • 2019-06-02
      • 2011-03-04
      • 1970-01-01
      • 2012-12-21
      • 2017-08-22
      相关资源
      最近更新 更多