I am making an iPhone app which requires the current Stock Prices.
I am receiving the data in CSV format from a link given below.
http://finance.yahoo.com/d/
Assuming you are asking how to parse the CSV data, here's a very simple bit of code to do it. Use google if you want to find more advanced parsers - they're out there..
// CsvParser2.h
#import
@interface NSString (CsvParser2)
// Returns an array of arrays for rows & columns
-(NSArray *)csvRows;
@end
// CsvParser2.m
#import "CsvParser2.h"
@implementation NSString (CsvParser2)
// Simplest possible implementation... NOT memory efficient!
// No apologies, no refunds...
-(NSArray *)csvRows
{
NSMutableArray* outRows = [NSMutableArray array];
NSCharacterSet* newlineCharSet = [NSCharacterSet newlineCharacterSet];
NSCharacterSet* separatorCharactersSet = [NSCharacterSet characterSetWithCharactersInString:@","];
NSArray* lines = [self componentsSeparatedByCharactersInSet:newlineCharSet];
for ( NSString* line in lines )
{
if ( !line.length ) continue;
NSArray* columns = [line componentsSeparatedByCharactersInSet:separatorCharactersSet];
if ( columns.count )
{
[outRows addObject:columns];
}
}
return outRows; // autoreleased
}
@end
This is defined as a category on NSString, to use it just do
NSMutableArray* myResponseAsArrayOfArraysOfStrings = [myCsvResponseAsAString csvRows];
for ( NSArray* row in myResponseAsArrayOfArraysOfStrings )
{
for ( NSString* column in row )
{
NSLog( "%@", column );
}
}
Hope this helps!
NOTE, this parser doesn't deal with quotes. Namely 1. If a field is quoted, the quotes are not stripped off. 2. If the quoted portion contains a comma the parser will split the line incorrectly.