Objective-C [[NSXMLDocument alloc] initWithContentsOfURL:url options:0 error:&error];帮助
我需要一点帮助,我有一个搜索字段和按钮可以触发此方法,但我收到下面的错误。
- (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
谁能帮忙,谢谢,萨米。
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您需要转义代码中的空格。例如,像这样
当我添加这个时,您的代码不会崩溃。
但一般来说,我建议将文件作为文件访问(例如 NSString stringWithContentsOfFile)。它会让你的生活更轻松。
您可以在此处查看网址不能包含的字符列表:
不安全:
由于多种原因,角色可能不安全。空间
字符是不安全的,因为重要的空格可能会消失并且
转录 URL 时可能会引入无关紧要的空格,或者
排版或经过文字处理程序处理。
You need to escape spaces in your code. E.g., like this
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.
更改此行:
对此:
阅读此问题以获取有关其工作原理的更多详细信息:NSError: 使用 nil 来检测 Error 实际上会关闭错误报告吗?
但是,您似乎根本不关心错误对象,所以您会更好只需像这样调用这些方法(将
&error
替换为NULL
)Change this line:
To this:
Read this question for additional detail as to why this works: NSError: Does using nil to detect Error actually turn off error reporting?
However, you don't seem to care about the error object at all so you would be better off just calling these methods like this (replace
&error
withNULL
)我想你可能需要:
I think you might need: