文件名NSString在空间中添加了不必要的%20 [英] File Name NSString adds unnecessary %20 in space

查看:112
本文介绍了文件名NSString在空间中添加了不必要的%20的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

已解决(感谢Regexident)

SOLVED (Thanks Regexident)

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

I have an app that passes the file path of a PDF to a custom -(id)init init method. It is added to the table and when it is selected, it calls the else statement for a non existent file:

- (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部分。

So whenever I have a document with no space in it's name, it pushes and inits. But when the file name has a space in it, it says it does not exist. And when I remove the if-else method, it init's, but crashes because the file doesn't exist. I've tried to replace the %20 in the file path with a regular space, but the method continues to call the else part.

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

So, is the file path not standardized and readable, or is my else method wrong?

推荐答案

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

As your path appears to be a percentage escaped path string I'd try this:

[fileURL stringByReplacingPercentEscapesUsingEncoding:NSUTF8StringEncoding]

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

And I'd rename fileURL to fileURLString to make clear that it's an NSString containing a URL path, not an actual NSURL (which your variable name would falsely imply).

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

But I'd check the code that populates _documentIconsURLs, as it's probabaly the origin of your problem.

这篇关于文件名NSString在空间中添加了不必要的%20的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆