iPhone Persistent Storage

前端 未结 3 1298
梦谈多话
梦谈多话 2021-02-11 09:06

I need to store certain information while my application is executing and again fetch it at the time the application starts. I tried storing it in XML using GData but didn\'t su

3条回答
  •  慢半拍i
    慢半拍i (楼主)
    2021-02-11 09:15

    You're trying to write to the resource directory which you probably don't have permission to do. Try changing the first line of your code to point to the document directory:

    NSArray *savePaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSMutableString *savePath = [NSMutableString stringWithString:[savePaths objectAtIndex:0]];
    [savePath appendString:@"/myFile.txt"];
    

    Also, you don't need to worry about file handles. NSData is capable of writing itself to disk:

    BOOL result = [data writeToFile:savePath atomically:YES];
    

提交回复
热议问题