gpt4 book ai didi

ios - 将 NSArray 组件转换为整数或十进制数

转载 作者:塔克拉玛干 更新时间:2023-11-02 09:50:48 25 4
gpt4 key购买 nike

我有一个案例,从应用程序中的 CSV 文件读取的数据必须转换为整数,必须稍后绘制。目前它无法识别何时将数据另存为

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 应该是整数。

非常感谢。

最佳答案

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

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

你只需要获取'components'的每个成员并得到它的intValue,就像这样:

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

关于ios - 将 NSArray 组件转换为整数或十进制数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9038317/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com