我想从每天更新的网络服务器解析 csv。我正在使用此链接 https://github.com/davedelong/CHCSVParser 中的 csvparser我正在使用这段代码:
NSError *err = [[[NSError alloc] init] autorelease];
NSString *lunchFileURL = [[NSString stringWithFormat:@"http://www.somewhere.com/LunchSpecials.csv"] stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
NSString *lunchFile = [NSString stringWithContentsOfURL:[NSURL URLWithString:lunchFileURL] encoding:NSUTF8StringEncoding error:&err];
CHCSVParser *p = [[CHCSVParser alloc] initWithContentsOfCSVString:lunchFile usedEncoding:&encoding error:nil];
我收到此错误:
No visible @interface for 'CHCSVParser' declares the selector 'initWithContentsOfCSVString:usedEncoding:error:'
我检查了此链接Load remote csv into CHCSVParser它不起作用。我是 ios 新手,请让我知道如何解决这个问题。非常感谢您的帮助。提前致谢。
最佳答案
大概应该是:
NSError *err;
NSString *lunchFileURL = [[NSString stringWithFormat:@"http://www.somewhere.com/LunchSpecials.csv"] stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
NSString *lunchFile = [NSString stringWithContentsOfURL:[NSURL URLWithString:lunchFileURL]
encoding:NSUTF8StringEncoding
error:&err];
// if you're going to create a CHCSVParser yourself, the syntax is:
CHCSVParser *p = [[CHCSVParser alloc] initWithCSVString:lunchFile
encoding:NSUTF8StringEncoding
error:&err];
// or, if you're going to use NSArray+CHCSVAdditions.h, the syntax might be:
NSArray *array = [[NSArray alloc] initWithContentsOfCSVString:lunchFile
encoding:NSUTF8StringEncoding
error:&err];
注意:
您不需要
alloc
/init
NSError
对象;这些带有NSError **
参数的类会在遇到错误时为您创建自动释放错误对象;否则他们就不管它;CHCSVParser
类方法不是initWithContentsOfCSVString
,而是initWithCSVString
;或者,如果您使用
NSArray
类扩展,则语法为arrayWithContentsOfCSVString
或initWithContentsOfCSVString
;和您指定了
&encoding
的编码,但此参数不是指针,因此我不知道这怎么可能是正确的;我刚刚指定了编码。
我假设您想使用 NSArray+CHCSVAdditions
类别方法 initWithContentsOfCSVString
或 arrayWithContentsOfCSVString
(它为您提供 autorelease
code> 对象),而不是 CHCSVParser
,但这取决于您。
关于iphone - CHCSVParser - 如何从网络服务器加载,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13891301/