I need a little help, i have a search field and button which trigger this method but i get the error underneath.
- (IBAction)fetchTracks:(id)sender {
NSString *input = [searchField stringValue];
NSString *searchString = [input stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
NSLog(@"searchString: %@", searchString);
NSError *error;
NSString* libraryPath = [[NSString alloc] initWithString:@"~/Music/iTunes/iTunes Music Library.xml"];
NSURL *url = [NSURL URLWithString:libraryPath];
[doc release];
doc = [[NSXMLDocument alloc] initWithContentsOfURL:url options:0 error:&error];
NSLog(@"doc: %@", doc);
[itemNodes release];
itemNodes = [[doc nodesForXPath:@"ItemSearchResponse/Items/Item" error:&error] retain];
[tableView reloadData];
}
* -[NSXMLDocument initWithContentsOfURL:options:error:]: nil argument
Can anyone help, thanks, Sami.
You need to escape spaces in your code. E.g., like this
libraryPath = [libraryPath stringByAddingPercentEscapesUsingEncoding: NSUTF8StringEncoding];
When I add this, your code doesn't crash.
But generally, I would recommend accessing files as files (e.g. NSString stringWithContentsOfFile
). It will make your life easier.
You can see the list of characters URL can't contain here:
Unsafe:
Characters can be unsafe for a number of reasons. The space character is unsafe because significant spaces may disappear and insignificant spaces may be introduced when URLs are transcribed or typeset or subjected to the treatment of word-processing programs.