I am having difficulty getting "removingPercentEncoding" working for an NSString in Cocoa and for String in Swift
For instance I have a text file of strings (one per line) similar to sample below.
"http://www.ebay.com/itm/like/272309198212?lpid=82&chn=ps&ul_ref=http%3A%2F%2Frover.ebay.com%2Frover%2F1%2F711-117182-37290-0%2F2%3Fmtid%3D1588%26kwid%3D1%26crlp%3D110544474129_324272%26itemid%3D272309198212%26targetid..."
My Swift code
let theFile = NSHomeDirectory() + "/urls.txt"
let aString = try String(contentsOfFile:theFile, encoding: String.Encoding.utf8)
let myStrings = aString.components(separatedBy: .newlines)
for theURL in myStrings {
print( (theURL.removingPercentEncoding)!)
}
My Cocoa code
NSUInteger i;
NSString *theFile = [NSString stringWithUTF8String:argv[1]];
NSCharacterSet *newlineCharSet = [NSCharacterSet newlineCharacterSet];
NSString* fileContents = [NSString stringWithContentsOfFile:theFile encoding:NSUTF8StringEncoding error:nil];
NSArray *lines = [fileContents componentsSeparatedByCharactersInSet:newlineCharSet];
for( i=0; i<[lines count]; i++ ) {
NSString *str = [lines objectAtIndex:i];
NSLog(@"%@\n", [str stringByRemovingPercentEncoding]);
}
Both of these example iterate through the lines of the file just fine but will not remove the % encodings. I've tried all the different string encodings with the same negative results:
Now when a do a string assignment like below, it will give the expected results, but not when iterating through a file
NSString *theString = @"http://www.ebay.com/itm/like/272309198212?lpid=82&chn=ps&ul_ref=http%3A%2F%2Frover.ebay.com%2Frover%2F1%2F711-117182-37290-0%2F2%3Fmtid%3D1588%26kwid%3D1%26crlp%3D110544474129_324272%26itemid..."
NSLog(@"%@", [theString stringbyRemovingPercentEncoding]);
I've got the nagging feeling, I'm missing something very simple. Or am I not crazy and this is some kind of bug.