0

I'm having a hard time getting my NSURL to work, when I create the final string before converting to URL it adds unwanted character to the end of the string, why is this happening and how can I fix it?

Here is my code:

NSString *remotepathstring = [[NSString alloc] init];
remotepathstring=newdata.remotepath;
NSLog(@"remotepathstring = %@",remotepathstring);

NSString *remotepathstringwithescapes = [remotepathstring stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
NSLog(@"remotepathstring = %@",remotepathstringwithescapes);

remotepathURL =[NSURL URLWithString:remotepathstringwithescapes];
NSLog(@"RemotePathUrl=%@",remotepathURL);

Log outputs as follows:

"remotepathstring = http://nalahandthepinktiger.com/wp-content/uploads/nalah-sheet-5.pdf‎"

"remotepathstring = http://nalahandthepinktiger.com/wp-content/uploads/nalah-sheet-5.pdf%E2%80%8E"

"RemotePathUrl=http://nalahandthepinktiger.com/wp-content/uploads/nalah-sheet-5.pdf%E2%80%8E"
Mike Abdullah
  • 14,933
  • 2
  • 50
  • 75
Bogiematch
  • 27
  • 7

1 Answers1

0

The sequence %E2%80%8E is a Unicode LEFT-TO-RIGHT MARK. This is present in your original remotepathstring, but invisible when printed out via NSLog.

The question becomes: how does newdata.remotepath get populated in the first place? Somewhere along the line it sounds like you need to perform some extra cleanup of input strings to strip out such a character.

Unrelated to the core question, it would seem you're a newcomer to Objective-C. This code is redundant and wasteful:

NSString *remotepathstring = [[NSString alloc] init];
remotepathstring=newdata.remotepath;

You create a string, only to immediately throw it away and replace it with another. If you're not using ARC, this has the additional problem of leaking! Instead do:

NSString *remotepathstring = newdata.remotepath;
Mike Abdullah
  • 14,933
  • 2
  • 50
  • 75
  • newdata.remotepath is populated from an sqlite database as follows: `newdata.remotepath = [[NSString alloc] initWithUTF8String:(char *)sqlite3_column_text(st,1)];` – Bogiematch May 07 '13 at 07:04
  • Then you need to sanitise these strings as they go into your database, or on the way out – Mike Abdullah May 07 '13 at 08:13
  • is there a recommended method to doing this? I can do it as they get retrieved. – Bogiematch May 07 '13 at 13:40
  • Nothing especially comes to mind. This is not something I've ever come across before, which suggests you need to ask yourself how such strings are making it into your database in the first place – Mike Abdullah May 07 '13 at 21:52