2014-09-30 35 views
1

我有一個問題,我無法在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

回答

1

如果您想查找可寫位置,請不要使用包路徑。這指向應用程序,它是隻讀的。

使用...

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

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

+0

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