开发者

How do I use a CSV file received from a URL query in Objective-C?

开发者 https://www.devze.com 2022-12-23 12:34 出处:网络
How do I use comma-separated-value (CSV) file received from a URL query in Objective-C? When I query the URL I get CSV data such as

How do I use comma-separated-value (CSV) file received from a URL query in Objective-C? When I query the URL I get CSV data such as

"OMRUAH=X",20.741,"3/16/开发者_如何转开发2010","1:52pm",20.7226,20.7594

How do I parse and use this for my application?

My problem is creating/initializing that NSString object in the first place. An example is this link http://download.finance.yahoo.com/d/quotes.csv?s=GBPEUR=X&f=sl1d1t1ba&e=.csv which returns CSV. I don't know how to parse this into an object since I cannot use an NSXMLParser object.


You can try the following code using -componentsSeparatedByString: which will end up with an NSArray of each component separated by:

NSURL *url = [NSURL URLWithString:@"http://download.finance.yahoo.com/d/quotes.csv?s=GBPEUR=X&f=sl1d1t1ba&e=.csv"];
NSString *reply = [NSString stringWithContentsOfURL:url encoding:NSASCIIStringEncoding error:nil];
NSArray *csvItems = [reply componentsSeparatedByString:@","];

Claus


A couple of tools:

  1. http://michael.stapelberg.de/cCSVParse
  2. http://www.cocoadev.com/index.pl?ReadWriteCSVAndTSV

Here's an example of using NSScanner: http://www.macresearch.org/cocoa-scientists-part-xxvi-parsing-csv-data

0

精彩评论

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