ios – 文件名NSString在空间中添加了不必要的内容

前端之家收集整理的这篇文章主要介绍了ios – 文件名NSString在空间中添加了不必要的内容前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
解决(感谢 Regexident)

我有一个应用程序将PDF的文件路径传递给自定义 – (id)init init方法.它被添加到表中,当它被选中时,它会为不存在的文件调用else语句:

  1. - (void) gridView:(AQGridView *)gridView didSelectItemAtIndex:(NSUInteger)index {
  2.  
  3. NSLog (@"Selected theArgument=%d\n",index);
  4.  
  5. UIViewController *viewController = [[[UIViewController alloc]init]autorelease];
  6. {
  7. //if file is built-in,read from the bundle
  8. if (index <= 3)
  9. {
  10. // first section is our build-in documents
  11. NSString *fileURLs = [_documentIconsURLs objectAtIndex:index];
  12. NSLog(@"file url -%@",fileURLs);
  13. viewController = [[[xSheetMusicViewController alloc]initWithContentURL:fileURLs]autorelease];
  14. }
  15. //if file is external,read from URL
  16. else
  17. {
  18. // second section is the contents of the Documents folder
  19. NSString *fileURL = [_documentIconsURLs objectAtIndex:index];
  20. NSLog(@"file url -%@",fileURL);
  21. NSString *path;
  22. NSString *documentsDirectoryPath = [self applicationDocumentsDirectory];
  23. NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask,YES);
  24. path = [[paths objectAtIndex:0] stringByAppendingPathComponent:fileURL];
  25.  
  26.  
  27. if ([[NSFileManager defaultManager] fileExistsAtPath:documentsDirectoryPath])
  28. {
  29. viewController = [[[xSheetMusicViewController alloc]initWithDocumentURL:fileURL]autorelease];
  30. }
  31. else
  32. {
  33. UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Failure!" message:@"The Selected File Does Not Exist"
  34. delegate:nil
  35. cancelButtonTitle:@"OK"
  36. otherButtonTitles: nil];
  37. [alert show];
  38. [alert release];
  39. return;
  40. }
  41. }
  42. [self.navigationController setNavigationBarHidden:YES animated:NO];
  43. [UIView beginAnimations:@"animation" context:nil];
  44. [UIView setAnimationDuration:1];
  45. [UIView setAnimationTransition:UIViewAnimationTransitionCurlDown forView:self.navigationController.view cache:YES];
  46. [self.navigationController pushViewController:viewController animated:NO];
  47. [UIView commitAnimations];
  48. }
  49. }

因此,每当我的文档中没有空格时,它就会推动并进入.但是当文件名中有空格时,它表示它不存在.当我删除if-else方法时,它会初始化,但崩溃因为文件不存在.我试图用常规空间替换文件路径,但该方法继续调用else部分.

那么,文件路径是不标准化和可读的,还是我的其他方法错了?

解决方法

由于您的路径似乎是一个百分比转义路径字符串,我试试这个:
  1. [fileURL stringByReplacingPercentEscapesUsingEncoding:NSUTF8StringEncoding]

我将fileURL重命名为fileURLString,以明确它是一个包含URL路径的NSString,而不是一个实际的NSURL(你的变量名称错误地暗示).

但我会检查填充_documentIconsURLs的代码,因为它可能是您问题的根源.

猜你在找的iOS相关文章