Objective-C [[NSXMLDocument alloc] initWithContentsOfURL:url options:0 error:&error];帮助

发布于 2024-10-17 08:22:39 字数 833 浏览 3 评论 0原文

我需要一点帮助,我有一个搜索字段和按钮可以触发此方法,但我收到下面的错误。

- (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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

孤独难免 2024-10-24 08:22:39

您需要转义代码中的空格。例如,像这样

  libraryPath = [libraryPath stringByAddingPercentEscapesUsingEncoding: NSUTF8StringEncoding];

当我添加这个时,您的代码不会崩溃。
但一般来说,我建议将文件作为文件访问(例如 NSString stringWithContentsOfFile)。它会让你的生活更轻松。

您可以在此处查看网址不能包含的字符列表:

不安全:

由于多种原因,角色可能不安全。空间
字符是不安全的,因为重要的空格可能会消失并且
转录 URL 时可能会引入无关紧要的空格,或者
排版或经过文字处理程序处理。

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.

九歌凝 2024-10-24 08:22:39

更改此行:

NSError *error;

对此:

NSError *error = nil;

阅读此问题以获取有关其工作原理的更多详细信息:NSError: 使用 nil 来检测 Error 实际上会关闭错误报告吗?


但是,您似乎根本不关心错误对象,所以您会更好只需像这样调用这些方法(将 &error 替换为 NULL

doc = [[NSXMLDocument alloc] initWithContentsOfURL:url options:0 error:NULL];
...
itemNodes = [[doc nodesForXPath:@"ItemSearchResponse/Items/Item" error:NULL] retain];

Change this line:

NSError *error;

To this:

NSError *error = nil;

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 with NULL)

doc = [[NSXMLDocument alloc] initWithContentsOfURL:url options:0 error:NULL];
...
itemNodes = [[doc nodesForXPath:@"ItemSearchResponse/Items/Item" error:NULL] retain];
妄司 2024-10-24 08:22:39

我想你可能需要:

NSString *urlString = [@"~/Music/iTunes/iTunes Music Library.xml" stringByExpandingTildeInPath];
NSString* libraryPath = [[NSString alloc] initWithString:urlString];

I think you might need:

NSString *urlString = [@"~/Music/iTunes/iTunes Music Library.xml" stringByExpandingTildeInPath];
NSString* libraryPath = [[NSString alloc] initWithString:urlString];
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文