检查文档目录 Url 是否为空

Check if Documents Directory Url empty

在下面的代码中,我试图检查我的文档目录 Url 是否为空,因此如果它是空的,它将立即显示或继续执行我需要的操作。

这是我的代码:

- (IBAction)saveVideo:(id)sender {
    NSURL *vedioURL;
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *documentsDirectory = [paths objectAtIndex:0];
    NSArray *filePathsArray = [[NSFileManager defaultManager] subpathsOfDirectoryAtPath:documentsDirectory  error:nil];
    NSLog(@"files array %@", filePathsArray);

    NSString *fullpath;
    for ( NSString *apath in filePathsArray )
    {
        fullpath = [documentsDirectory stringByAppendingPathComponent:apath];
        vedioURL =[NSURL fileURLWithPath:fullpath];
    }
    NSLog(@"vurl %@",vedioURL);
    NSURL *movieUrl = [NSURL fileURLWithPath:fullpath];
    NSString *moviePath = [movieUrl path];

    if (videoUrl.absoluteString.length == 0) {
        NSLog(@"No way");
    } else {
        self.imageViewPost.image = [self generateThumbImage:moviePath];
        AVPlayerItem * playeritem = [AVPlayerItem playerItemWithURL:vedioURL];
        [_player replaceCurrentItemWithPlayerItem:playeritem];
        [_player play];
        [self clearTmpDirectory];
    }
}

应用程序崩溃,它向我的 Url:

显示 Null 值
vurl (null)
2017-11-29 20:54:24.288473+0400 EasyVideo[43587:2429960] ***
Terminating app due to uncaught exception 'NSInvalidArgumentException',
 reason: '*** -[NSURL initFileURLWithPath:]: nil string parameter'

希望能解决这个问题。 谢谢

你可以先从URL中获取数据,检查它是否不为空。

NSData *data = [NSData dataWithContentsOfURL:videoURL];

此处,如果数据不为空则执行相应的任务:

 if (data == nil) 
  {
       //Do not perform the task.

  }
else 
  {
      //perform the task
  }