English 中文(简体)
Objective-C[[NSXMLDocument alloc]initWithContentsOfURL:url选项:0错误:&错误];帮助
原标题:Objective-C [[NSXMLDocument alloc] initWithContentsOfURL:url options:0 error:&error]; help

我需要一点帮助,我有一个搜索字段和按钮来触发这个方法,但我在下面得到了错误。

- (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:选项:错误:]:无参数

有人能帮忙吗,谢谢,萨米。

最佳回答

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

  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.

您可以看到URL不能包含的字符列表此处

不安全:

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 *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];

我认为您可能需要:

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




相关问题
how to represent it in dtd?

I have two element action and guid. guid is a required field when action is add. but when action is del it will not appear in file. How to represent this in dtd ?

.Net application configuration add xml-data

I need to add xml-content to my application configuration file. Is there a way to add it directly to the appSettings section or do I need to implement a configSection? Is it possible to add the xml ...

XStream serializing collections

I have a class structure that I would like to serialize with Xstream. The root class contains a collection of other objects (of varying types). I would like to only serialize part of the objects that ...

MS Word splits words in its XML format

I have a Word 2003 document saved as a XML in WordProcessingML format. It contains few placeholders which will be dynamically replaced by an appropriate content. But, the problem is that Word ...

Merging an XML file with a list of changes

I have two XML files that are generated by another application I have no control over. The first is a settings file, and the second is a list of changes that should be applied to the first. Main ...

How do I check if a node has no siblings?

I have a org.w3c.dom.Node object. I would like to see if it has any other siblings. Here s what I have tried: Node sibling = node.getNextSibling(); if(sibling == null) return true; else ...

Ordering a hash to xml: Rails

I m building an xml document from a hash. The xml attributes need to be in order. How can this be accomplished? hash.to_xml

热门标签