56597

File Name NSString adds unnecessary %20 in space

Question:

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?

Answer1:

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.

Recommend

  • String to Hash conversion
  • Transforming recursion into tail recursion?
  • what is the #! sign in the url what does it mean? [duplicate]
  • Exception handling in pl/sql
  • Unmarshal json into struct: cannot unmarshal array into Go value
  • How do I target each menu different items on active state in Wordpress
  • Javascript - Adding Full Screen Video to Image Slideshow
  • q - apply function on table rowwise
  • php using msaccess
  • JavaScript Interface not working in my Android Application
  • Define a symbol for an assembly (.s) source file in Android.mk?
  • Show QMainwindow in the middle of the screen
  • Failure to Read Updated AnyLogic DB Values
  • Android Studio generate signed apk not working on build 0.3.6 - Giving Error
  • Parse fasta sequence to the dictionary
  • What is the function of Spring Session?
  • How to send binary with spring boot websocket?
  • How detect the correct modification of a file?
  • ASP.Net MVC entity framework submit model, then open new model in edit page
  • Copy Row if Cell Contains X to Different Sheet then delete row.
  • Two instances of blur.js possible?
  • How to split a string into a list by digits? [duplicate]
  • How to order the ties in data so that the previously observed value appears first
  • Move UIView per UIBezierPath [closed]
  • Raphael-GWT: fill image of a shape (Rect) appears offset. How to resolve this?
  • Oracle REF can't go through
  • Can someone explain how Yii minimizing assets is supposed to work on Heroku?
  • Exporting Records from Acumatica via REST Contract-Based API
  • Custom Timeline items in Glimpse
  • How to create OLE Automation to be used with ClearExplorer
  • Signed Java web start application with Glassfish 4.1 and Java7
  • How to use array in autohotkey?
  • Ruby regex for matching simpliest Ruby's regexes
  • JavaScript Regex to Match Boundaries of Words with diacritics
  • Android: Unable to detect vertical plane
  • Debug `Unexpected end of JSON input Error` on content script