已解决(感谢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方法时,它就是init,但崩溃是因为该文件不存在。我试图用常规空格替换文件路径中的%20,但该方法继续调用else部分。
那么,文件路径是不标准化和可读的,还是我的其他方法错了?
答案 0 :(得分:17)
由于你的路径似乎是一个百分比转义路径字符串,我试试这个:
[fileURL stringByReplacingPercentEscapesUsingEncoding:NSUTF8StringEncoding]
我将fileURL
重命名为fileURLString
以明确它是一个包含URL路径的NSString,而不是实际的NSURL(您的变量名称将错误地暗示)。
但是我会检查填充_documentIconsURLs
的代码,因为它可能是您问题的根源。