2011-11-12 17 views
7

SOLVED (Cảm ơn Regexident)File Name NSString thêm không cần thiết% 20 trong không gian

Tôi có một ứng dụng mà vượt qua đường dẫn tập tin của một PDF sang một phương pháp init tùy chỉnh -(id)init. Nó được bổ sung vào bảng và khi nó được chọn, nó gọi các báo cáo khác cho một tập tin không tồn tại:

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

Vì vậy, bất cứ khi nào tôi có một tài liệu với không gian trong tên của nó, nó đẩy và trong tệp. Nhưng khi tên tập tin có một khoảng trống trong nó, nó nói nó không tồn tại. Và khi tôi xóa phương thức if-else, nó init, nhưng bị treo vì tệp không tồn tại. Tôi đã cố gắng để thay thế% 20 trong đường dẫn tập tin với một không gian thường xuyên, nhưng phương pháp tiếp tục gọi phần khác.

Vì vậy, đường dẫn tệp không được chuẩn hóa và có thể đọc được hay phương pháp khác của tôi có sai không?

+0

Đánh giá bằng "% 20" và tên biến ("... URL") trong mã của bạn có vẻ như bạn đang nhầm lẫn URL tệp (mã hóa khoảng trắng là "% 20", v.v.) với đường dẫn tệp (sử dụng dấu cách, v.v.) – Regexident

+0

url của tệp được chuyển thành chuỗi trong phương thức táo từ ví dụ documentInteraction. Tuy nhiên, url được chuyển đến bộ điều khiển không phải DI. Vì vậy, làm thế nào để sửa chữa nó? – CodaFi

Trả lời

16

Như con đường của bạn dường như là một tỷ lệ phần trăm thoát chuỗi con đường tôi muốn thử điều này:

[fileURL stringByReplacingPercentEscapesUsingEncoding:NSUTF8StringEncoding] 

Và tôi muốn đổi tên fileURL-fileURLString để làm cho rõ ràng rằng đó là một NSString chứa một đường dẫn URL, không phải là một NSURL thực tế (tên biến của bạn sẽ ngụ ý sai).

Nhưng tôi muốn kiểm tra mã điền _documentIconsURLs, vì nó là nguyên nhân gốc rễ của sự cố của bạn.

+0

Rất vui! Nhưng đề nghị trước đó của bạn là đúng. Tôi đã gọi [filename lastpathcomponent] thay vì [filename path]; Tôi vẫn sẽ bỏ phiếu cho bạn mặc dù. Cảm ơn bạn. – CodaFi

Các vấn đề liên quan