开发者

How do I use comma-separated-values received from a URL query in Objective-c?

开发者 https://www.devze.com 2022-12-23 19:03 出处:网络
How do I use comma-separated-values received from a URL query in Objective-c? when I query the URL I get csv such as (\"OMRUAH=X\",20.741,\"3/16/2010\",\"1:52pm\",20.7226,20.7594).

How do I use comma-separated-values received from a URL query in Objective-c? when I query the URL I get csv such as ("OMRUAH=X",20.741,"3/16/2010","1:52pm",20.7226,20.7594). How do I capture and 开发者_Python百科use this for my application?


You have two options:

Use a CSV parser: http://freshmeat.net/projects/ccsvparse

Or parse the data yourself into an array:

// myString is an NSString object containing your data
NSArray *array = [myString componentsSeparatedByString: @","];


I recently dealt with CSV parsing for Yahoo! Finance as well. I used Ragel to write a parser in C that was good enough for the CSV I was getting. It handled everything but escaped quotes, which are not going to show up much in stock quotes. It was pretty painless and a good learning experience. I'd post the code, but it was work-for-hire, so I don't own it.

Turning a C string into an NSString is easy. If you have it as an NSData, as you likely do at the end of a URL download, just do [[NSString alloc] initWithData:csvData encoding:NSUTF8StringEncoding]. If you have a pointer to a character buffer instead, use [[NSString alloc] initWithBytes:buffer length:buflen encoding:NSUTF8StringEncoding]. buflen could be strlen(buffer) if buffer is a normal, NUL-terminated C string.

0

精彩评论

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