[英]What is the correct usage of arrayWithContentsOfURL?
This is a bit basic; 这有点基本; I am trying to retrieve http data for an iPhone app. 我正在尝试检索iPhone应用程序的http数据。
I have www.myhost.com/test.html that reads 我有www.myhost.com/test.html
<array><string>test</string><string>test2</string></array>
Then I have 然后我有
NSURL *baseURL = [NSURLRLWithString:@"http://www.myhost.com/test.html"];
NSArray *array = [NSArray arrayWithContentsOfURL:baseURL];
NSLog(@"%@", [array description]);
Which keeps returning (null). 哪个继续返回(null)。 What am I missing? 我错过了什么? Thanks! 谢谢!
It should probably be in a complete plist format (with doctype and all that) 它应该是一个完整的plist格式(使用doctype和所有)
easiest way to create a proper plist is with "Property List Editor.app" 创建合适的plist的最简单方法是使用“Property List Editor.app”
on a side note: NSLog(@"%@", [array description]);
旁注: NSLog(@"%@", [array description]);
is the same as NSLog(@"%@", array);
与NSLog(@"%@", array);
相同NSLog(@"%@", array);
The arrayWithContentsOfURL documentation clearly states that arrayWithContentsOfURL文档清楚地说明了这一点
The array representation at the location identified by aURL must contain only property list objects (NSString, NSData, NSArray, or NSDictionary objects). 由aURL标识的位置处的数组表示必须仅包含属性列表对象(NSString,NSData,NSArray或NSDictionary对象)。
That is, the kind of objects that you would obtain by calling the writeToURL:atomically method. 也就是说,通过调用writeToURL:atomically方法获得的对象类型。 The location written by this method can be used to initialize a new array with the class method arrayWithContentsOfURL:
or the instance method initWithContentsOfURL:
. 此方法写入的位置可用于使用类方法arrayWithContentsOfURL:
或实例方法initWithContentsOfURL:
初始化新数组。 Therefore, I suggest using these standard methods for writing and reading your arrays from an URL, instead of writing your own file and trying to read it by arrayWithContentsOfURL:
. 因此,我建议使用这些标准方法从URL写入和读取数组,而不是编写自己的文件并尝试通过arrayWithContentsOfURL:
读取它arrayWithContentsOfURL:
。
One additional discovery I made, is that if you are trying to build the list on the fly, to be served over the web rather than coming from a static file you create with the plist editor you must escape the strings. 我做的另一个发现是,如果你试图动态构建列表,通过网络提供而不是来自你使用plist编辑器创建的静态文件,你必须逃避字符串。 If the document you has any xml errors in it your array will not load. 如果文档中有任何xml错误,则无法加载数组。 For example I had an & in some of my strings, and was still getting an empty array until I escaped the string and replaced & with &
例如,我有一个&在我的一些字符串中,并且仍然得到一个空数组,直到我转义字符串并替换&与&
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.