我有一个简单的对象。它有几个NSString属性(propertyA,propertyB,propertyC)。Objective-C:将字符串解析为对象
我有以下形式的字符串(从CSV文件中读取):
this is value A, this is value B, this is value C
another row A, another row B
注意,第二行丢失最后一个属性。
我想解析字符串到我的对象。目前,我抓住从CSV文件一行,这样做:
MyObject *something = [[MyObject alloc] init];
NSArray *split = [line componentsSeparatedByString:@","];
if (something.count > 0)
something.propertyA = [split objectAtIndex:0];
if (something.count > 1)
something.propertyB = [split objectAtIndex:1];
if (something.count > 2)
something.propertyC = [split objectAtIndex:2];
这个效果很好,但感觉很可怕的,哈克! 有没有人对我如何改进代码有任何建议?
CSV是一个肮脏的文件格式,让你有一个肮脏的代码来解析。但是,你可以将数据文件从csv更改为json吗? – CarlJ 2012-01-02 14:54:27
csv非常可怕...在这种情况下使用它有很好的理由。 – NeilD 2012-01-02 14:56:36
所以只有肮脏的方式... – CarlJ 2012-01-02 14:57:14