This is a bit basic; I am trying to retrieve http data for an iPhone app.
I have www.myhost.com/test.html that r开发者_JS百科eads
<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). What am I missing? Thanks!
It should probably be in a complete plist format (with doctype and all that)
The man page
easiest way to create a proper plist is with "Property List Editor.app"
on a side note: NSLog(@"%@", [array description]);
is the same as NSLog(@"%@", array);
The arrayWithContentsOfURL documentation clearly states that
The array representation at the location identified by aURL must contain only property list objects (NSString, NSData, NSArray, or NSDictionary objects).
That is, the kind of objects that you would obtain by calling the writeToURL:atomically method. The location written by this method can be used to initialize a new array with the class method arrayWithContentsOfURL:
or the instance method 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:
.
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. If the document you has any xml errors in it your array will not load. 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 &
精彩评论