2012-01-27 79 views
2

我有一种情况,即从应用程序中的CSV文件读取的数据必须转换为整数,稍后必须绘制。目前它不能识别何时将数据保存为将NSArray组件转换为整数或十进制数

int i=[[rows objectAtIndex:0] componentsSeparatedByString:@","]; 

这是实施的代码。

-(void)connection :(NSURLConnection *) connection didReceiveData:(NSData *)data{ 

    [self serverConnect]; 
    response = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding]; 
    //NSLog(response); 

    NSString *stripped1 = [response stringByReplacingOccurrencesOfString:@"\r" withString:@""]; 
    NSArray *rows = [stripped1 componentsSeparatedByString:@"\n"]; 

    NSArray *components; 

    for (int i=0;i<[rows count]; i++) { 
     if(i == 0 || [[rows objectAtIndex:i] isEqualToString:@""]){ 
      continue; 
     } 
     components = [[rows objectAtIndex:i] componentsSeparatedByString:@","]; 
     NSLog(@"data1:%@ data2:%@ data3:%@", [components objectAtIndex:0] ,[components objectAtIndex:1],[components objectAtIndex:2]); 
    } 

data1data2data3应该是整数。

非常感谢。

回答

3

componentsSeparatedByString返回子字符串或NSString的实例。

  components = [[rows objectAtIndex:i] componentsSeparatedByString:@","]; 

你只需要采取“分量”的每个成员,并得到它的intValue,像这样:

int myInt = [[components objectAtIndex:n] intValue]; 
1

的NSArray和NSMutableArray中只能包含的对象。所以从它得到整数值,使用[object intValue]。如果您需要向数组添加整数,请从整数中创建一个NSNumber对象并插入它。我知道Rayfleck回答了你的问题,我只想指出数组在iOS中的工作方式。希望这可以帮助。