What is the correct use of arrayWithContentsOfURL?

This is a bit basic; I am trying to get http data for an iPhone application.

I have a www.myhost.com/test.html that reads

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

Returns (null). What am I missing? Thanks!

+4
source share
3 answers

It should probably be in full plist format (with doctype and all that)

Manual page

The easiest way to create the correct plist is with the "List of Properties Editor.app"

on side note: NSLog(@"%@", [array description]); matches NSLog(@"%@", array);

+5
source

The documentation for arrayWithContentsOfURL clearly states that

The location representation of the array identified by aURL should only contain property list objects (NSString, NSData, NSArray, or NSDictionary Objects).

That is, the objects you would get by calling the writeToURL: atomically method . The location written by this method can be used to initialize a new array using the class method arrayWithContentsOfURL: or the instance initWithContentsOfURL: Therefore, I suggest using these standard methods to write and read your arrays from a URL, instead of writing your own file and trying to read it with arrayWithContentsOfURL:

+5
source

One of the additional discoveries that I made is that if you are trying to create a list on the fly to be serviced over the Internet, and not from a static file created using the plist editor, you should avoid strings. If in the document you have xml errors in it, your array will not load. For example, I had one in some of my lines, and I still got an empty array until I ran away from the line and replaced it with &amp;

0
source

All Articles