无法获取文件的路径

时间:2013-11-08 13:16:15

标签: iphone objective-c cocoa-touch ios7

代码工作100%但是在转换到iOS7和XCode 5之后我无法使用NSFilemanager方法获取文件的路径,代码找不到timetableXML文件和触发器的路径 - > else @“没有这样的文件存在”,这里有代码:

- (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");
        }         

    });

}

1 个答案:

答案 0 :(得分:0)

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

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

但是在使用不同的代码创建测试路径时:

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

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

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