文件名 NSString 在空格中添加了不必要的 %20

发布于 2024-12-15 07:38:43 字数 2539 浏览 0 评论 0原文

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

SOLVED (Thanks Regexident)

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];  
    }
}

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

儭儭莪哋寶赑 2024-12-22 07:38:43

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

[fileURL stringByReplacingPercentEscapesUsingEncoding:NSUTF8StringEncoding]

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

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

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

[fileURL stringByReplacingPercentEscapesUsingEncoding:NSUTF8StringEncoding]

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).

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文