我有一个案例,从应用程序中的 CSV 文件读取的数据必须转换为整数,必须稍后绘制。目前它无法识别数据何时保存为
int i=[[rows objectAtIndex:0] componentsSeparatedByString","];
这是实现的代码。
-(void)connection NSURLConnection *) connection didReceiveDataNSData *)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
、data2
和 data3
应该是整数。
非常感谢。
componentsSeparatedByString 返回子字符串或 NSString 的实例。
components = [[rows objectAtIndex:i] componentsSeparatedByString","];
您只需要获取“组件”的每个成员并获取它的 intValue,如下所示:
int myInt = [[components objectAtIndex:n] intValue];
关于ios - 将 NSArray 组件转换为整数或十进制数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9038317/
欢迎光临 OStack程序员社区-中国程序员成长平台 (https://ostack.cn/) | Powered by Discuz! X3.4 |