【发布时间】:2014-09-30 11:51:06
【问题描述】:
我有一个问题,我无法在 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 没有通过 throw,并且在 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];
}
【问题讨论】:
-
[SettingVO toFullPath:@"selected"]返回什么? -
在 iOS 8 中它返回
NULL但在 iOS 7 中它返回 "/Users/CANCAN/Library/Developer/CoreSimulator/Devices/BD971C52-6E84-4DDA-A90F-588476CFA2D1/data/Applications/ D5E87B2C-C027-4F53-88B1-6FFF989CD5D8/Documents/data/selected”路径@PhillipMills -
您可能使用过
toFullPath:中的调试器来找出它返回NULL 的原因。 -
@PhillipMills 我已经更新了我的问题,希望对您有所帮助。
标签: ios ios8 writetofile