2012-01-27 2 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]); 
    } 

data1, data2data3은 정수로되어있다.

고마워요.

답변

3

componentsSeparatedByString은 부분 문자열 또는 NSString의 인스턴스를 반환합니다.

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

당신은 너무처럼 '구성 요소'의 각 구성원을하고있는 intValue의 얻을 필요가 :

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

있는 NSArray와 NSMutableArray의 개체 만 포함 할 수 있습니다. 따라서 정수 값을 가져오고 [object intValue]를 사용하십시오. 배열에 정수를 추가해야하는 경우 정수에서 NSNumber 객체를 만들고 삽입합니다. 나는 Rayfleck이 당신의 질문에 답했고 단지 배열이 iOS에서 어떻게 작동 하는지를 지적하고자합니다. 희망이 도움이됩니다.

관련 문제