ios - 文件名 NSString 在空间中添加不必要的 %20

标签 ios ipad nsstring file-sharing

已解决(感谢 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方法有误?

最佳答案

由于您的路径似乎是百分比转义路径字符串,所以我会尝试这样做:

[fileURL stringByReplacingPercentEscapesUsingEncoding:NSUTF8StringEncoding]

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

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

关于ios - 文件名 NSString 在空间中添加不必要的 %20,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8107303/

相关文章:

iphone - Monotouch UIScrollView,放大时文本模糊

iphone - 如何开始一个同时输出 iPhone 和 iPad 的项目?

ios - Xcode,ios,将变量名称设置为另一个变量的值?

iphone - 从 NSString 中提取子字符串的线程

ios - swift 。如何在 ViewController 以外的单独的 swift 文件中编写 UICollectionView

ios - iOS 12 中通知中心下拉时计时器会中断

ios - 带 token 身份验证的 NSURLSession

ios - addSubView当前uiview

objective-c - 无法更改 UITableView 中的单元格大小

ios - NSString 根据数值拆分字符串