我有一个问题,我无法在iOS 8中保存,而它在iOS 7中工作正常。请问我的问题在哪里?
.h
NSMutableArray *selected;
.m
- (void)saveSelected
{
if (![selected writeToFile:[SettingVO toFullPath:@"selected"] atomically:YES])
{
NSLog(@"Could not save selected!!! %@", selectedParitems);
}
}
更新:我已经包含了toFullPath
方法并对其进行了调试。我发现在iOS 7中,如果fileMgr
没有通过抛出,并且在iOS 8下工作正常则返回nil。我希望这会有所帮助。
+ (NSString *)toFullPath:(NSString *)path
{
NSString *bundlePath = [[NSBundle mainBundle] bundlePath];
NSURL *url = [NSURL fileURLWithPath:bundlePath];
NSMutableArray *components = [NSMutableArray arrayWithArray:[url pathComponents]];
[components removeLastObject];
[components addObject:@"Documents"];
[components addObject:@"data"];
NSString *fullPath = [NSString pathWithComponents:components];
NSFileManager *fileMgr = [NSFileManager defaultManager];
if ( ![fileMgr fileExistsAtPath:fullPath isDirectory:NULL] )
{
if ([fileMgr createDirectoryAtPath:fullPath withIntermediateDirectories:NO attributes:nil error:nil])
{
//--N-SLog(@"+ Items directory created.");
}
else
{
//--N-SLog(@"- Couldn't create Items directory!!!");
return nil;
}
}
return [fullPath stringByAppendingPathComponent:path];
}
答案 0 :(得分:1)
如果要查找可写位置,请不要使用捆绑路径。这指向应用程序,它是只读的。
使用...
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *path = [paths firstObject];
if (path) {
// ...
}
...获取基本文档路径。