开发者

(iPhone) Reading from a web file - NSMutableArray

开发者 https://www.devze.com 2023-01-07 08:08 出处:网络
Right now I have an NSArray whose items are just written in the implementation file. I\'d like to know how to make it so I can sto开发者_运维百科re all the items on a web (text) file. Example:

Right now I have an NSArray whose items are just written in the implementation file.

I'd like to know how to make it so I can sto开发者_运维百科re all the items on a web (text) file. Example:

http://mysite.com/files/objects.txt

On that document:

Object 0 goes here
Object 1 goes here
Object 2 goes here
Object 3 goes here

etc. All separated by a line break.

How would I be able to do this? And when I update that text file, I'd like the objects to update as well. I'd do this via an NSMutable array, correct?

Thanks in advance.


- (NSArray*)getObjectsFromWeb {
    NSURL *url = [NSURL URLWithString:@"http://mysite.com/files/objects.txt"];
    NSStringEncoding usedEncoding;
    NSError *error;
    NSString *data = [NSString stringWithContentsOfURL:url
                                          usedEncoding:&usedEncoding
                                                 error:&error];

    return [data componentsSeparatedByString:@"\n"];
}

after data is assigned here, usedEncoding (and possibly error) will have a value that you can inspect and tak action on if you like.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号