Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the correct usage of arrayWithContentsOfURL?

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

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

Which keeps returning (null). What am I missing? Thanks!

like image 518
Mike A Avatar asked Dec 30 '22 08:12

Mike A


2 Answers

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

like image 91
cobbal Avatar answered Jan 28 '23 23:01

cobbal


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:.

like image 20
luvieere Avatar answered Jan 28 '23 23:01

luvieere