【问题标题】:File Name NSString adds unnecessary %20 in space文件名 NSString 在空间中添加了不必要的 %20
【发布时间】:2011-11-12 20:23:38
【问题描述】:

已解决(感谢 Regexident)

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

- (void) gridView:(AQGridView *)gridView didSelectItemAtIndex:(NSUInteger)index {

NSLog (@"Selected theArgument=%d\n", index);

UIViewController *viewController = [[[UIViewController alloc]init]autorelease];
{
    //if file is built-in, read from the bundle
    if (index <= 3)
    {
        // first section is our build-in documents
        NSString *fileURLs = [_documentIconsURLs objectAtIndex:index];   
        NSLog(@"file url -%@", fileURLs);
        viewController = [[[xSheetMusicViewController alloc]initWithContentURL:fileURLs]autorelease];
    }
    //if file is external, read from URL
    else
    {
        // second section is the contents of the Documents folder
        NSString *fileURL = [_documentIconsURLs objectAtIndex:index];
        NSLog(@"file url -%@", fileURL);
        NSString *path;
        NSString *documentsDirectoryPath = [self applicationDocumentsDirectory];
        NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
        path = [[paths objectAtIndex:0] stringByAppendingPathComponent:fileURL];


        if ([[NSFileManager defaultManager] fileExistsAtPath:documentsDirectoryPath])
        {
            viewController = [[[xSheetMusicViewController alloc]initWithDocumentURL:fileURL]autorelease];
        }
        else
        {
            UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Failure!"                                                            message:@"The Selected File Does Not Exist"
                                                           delegate:nil
                                                  cancelButtonTitle:@"OK"
                                                  otherButtonTitles: nil];
            [alert show];
            [alert release];        
            return;
        }
    }
[self.navigationController setNavigationBarHidden:YES animated:NO]; 
[UIView beginAnimations:@"animation" context:nil];
[UIView setAnimationDuration:1];
[UIView setAnimationTransition:UIViewAnimationTransitionCurlDown forView:self.navigationController.view cache:YES]; 
[self.navigationController pushViewController:viewController animated:NO];
[UIView commitAnimations];  
    }
}

所以每当我有一个名称中没有空格的文档时,它就会推送并初始化。但是当文件名中有空格时,它表示它不存在。当我删除 if-else 方法时,它会初始化,但由于文件不存在而崩溃。我尝试将文件路径中的 %20 替换为常规空格,但该方法继续调用 else 部分。

那么,是文件路径不规范,不可读,还是我的else方法不对?

【问题讨论】:

  • 从代码中的“%20”和变量命名(“...URL”)来看,您似乎将文件 URL(将空格编码为“%20”等)与文件混淆了路径(使用空格等)。
  • 文件 url 被转换为来自 documentInteraction 示例的苹果方法中的字符串。但是,该 url 被传递给非 DI 控制器。那么,我该如何解决呢?

标签: ios ipad nsstring file-sharing


【解决方案1】:

由于您的路径似乎是一个百分比转义的路径字符串,我会试试这个:

[fileURL stringByReplacingPercentEscapesUsingEncoding:NSUTF8StringEncoding]

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

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

【讨论】:

  • 不错的尝试!但是你之前的建议是对的。我正在调用 [filename lastpathcomponent] 而不是 [filename path];不过,我仍然会投票给你。谢谢。
猜你喜欢
  • 1970-01-01
  • 2012-07-24
  • 1970-01-01
  • 1970-01-01
  • 2017-11-08
  • 1970-01-01
  • 2020-08-23
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多