2012-01-27 12 views
2

アプリケーションのCSVファイルから読み取られたデータを整数に変換する必要がある場合があります。後でプロットする必要があります。現在のところ、データの保存時には認識されません。NSArrayコンポーネントを整数または10進数に変換する

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で配列がどのように動作するかを指摘したいと思います。お役に立てれば。

関連する問題