【问题标题】:Cannot writeToFile toFullPath in iOS 8 issue在 iOS 8 问题中无法 writeToFile toFullPath
【发布时间】: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


【解决方案1】:

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

使用...

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

...获取基本文档路径。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2011-09-20
    • 1970-01-01
    • 2015-07-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-11-19
    相关资源
    最近更新 更多