在iOS 8中无法将写入文件写入全路径问题

问题描述:

我有一个问题,我无法在iOS 8中保存,但它在iOS 7中工作正常。请问哪里是我的问题?在iOS 8中无法将写入文件写入全路径问题

.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

'[SettingVO toFullPath:@“选择”]'返回什么? – 2014-09-30 11:57:30

+0

在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 – CAN 2014-09-30 12:29:49

+0

你可以使用'toFullPath:'里面的调试器来找出它返回NULL的原因。 – 2014-09-30 12:31:18

如果您想查找可写位置,请不要使用包路径。这指向应用程序,它是只读的。

使用...

NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); 
NSString *path = [paths firstObject]; 
if (path) { 
    // ... 
} 

...得到基本的文件路径。

+0

它的工作原理非常感谢。 – CAN 2014-09-30 13:52:03