【问题标题】:Can't get the path of the file无法获取文件的路径
【发布时间】:2013-11-08 13:16:14
【问题描述】:

代码可以 100% 运行,但是在切换到 iOS7 和 XCode 5 后,我无法使用 NSFilemanager 方法获取文件的路径,代码无法找到 timetableXML 文件和触发器的路径 -> else @"NO这样的文件存在”,这里有代码:

- (void)viewDidLoad
{
    [super viewDidLoad];

    APUAppDelegate *appDelegate = (APUAppDelegate *)[[UIApplication sharedApplication]delegate];
    context = [appDelegate managedObjectContext];


    dispatch_queue_t queue = dispatch_get_global_queue(0,0);
    dispatch_async(queue, ^{
        NSLog(@"Beginning download");
        NSString *stringURL = @"http://webspace.apiit.edu.my/intake-timetable/download_timetable/timetableXML.zip";
        NSURL  *url = [NSURL URLWithString:stringURL];
        NSData *urlData = [NSData dataWithContentsOfURL:url];
        NSLog(@"Got the data!");
        //Find a cache directory. You could consider using documenets dir instead (depends on the data you are fetching)
        NSArray *paths = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES);
        NSString *path = [paths  objectAtIndex:0];

        //Save the data
        NSLog(@"Saving");
        dataPath = [path stringByAppendingPathComponent:@"timetableXML.zip"];
        dataPath = [dataPath stringByStandardizingPath];
        [urlData writeToFile:dataPath atomically:YES];

        /////////////////// TEST
        // Existence of the xml file

        NSFileManager *fileManager = [NSFileManager defaultManager];
        NSString *pathToMyFile = [path stringByAppendingPathComponent:@"timetableXML.zip"];
        if ([fileManager fileExistsAtPath:pathToMyFile]) {
            // file exists
            NSLog(@"YES a file exist with ZIP extension");
            NSLog(@"Unziped successfully!");
            path1 = pathToMyFile;
            [self unzipFile:path1];

        }
        else {
            NSLog(@"NO such a file exist");
        }         

    });

}

【问题讨论】:

    标签: iphone objective-c cocoa-touch ios7


    【解决方案1】:

    在编写文件时,使用以下代码创建路径:

    dataPath = [path stringByAppendingPathComponent:@"timetableXML.zip"];
    dataPath = [dataPath stringByStandardizingPath];
    

    但是当测试路径是用这个不一样的代码创建的:

    NSString *pathToMyFile = [path stringByAppendingPathComponent:@"timetableXML.zip"];
    

    NSLog() 两个路径并尝试找出问题。

    查看文件是否已写入。 注意,没有检查writeToFile:方法的状态,添加错误检查。

    【讨论】:

    • 感谢 Zaph 的解决方案。我检查了两条路径,它们是相同的。写入文件也是如此。我想知道的是我在 xcode 4 和 iOS 6 上使用备份的时间一切正常,但在 iOS 7 和 xcode 5 上出现了这个问题。但我必须检查更多。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2019-01-19
    • 2013-07-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-05-21
    • 2014-10-22
    相关资源
    最近更新 更多