1

代码可以 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");
        }         

    });

}
4

1 回答 1

0

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

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

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

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

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

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

于 2013-11-08T14:01:34.233 回答