[英]How to convert NSArray to integer values
In my iPhone by applying below code I am able to get value of "i" 在我的iPhone中应用下面的代码,我能够获得“i”的价值
NSMutableArray *Array = [NSMutableArray arrayWithCapacity:100];
for (NSUInteger i = 0; i < 10; i++) {
id a = [NSDecimalNumber numberWithDouble:i];
[Array addObject:[NSMutableDictionary dictionaryWithObjectsAndKeys:a, @"a", nil]];
}
But if I want to get value from array like as an example 但是,如果我想从数组中获取值,就像一个例子
NSArray * numberArray = [[NSArray alloc] initWithObjects:@"0.1",@"0.2",@"0.5",nil];
and then if I want to use it as 如果我想用它作为
NSMutableArray *Array = [NSMutableArray arrayWithCapacity:100];
for (NSUInteger i = 0; i < 10; i++) {
id a = [NSDecimalNumber numberWithDouble:[numberArray objectAtIndex:i]];
[Array addObject:[NSMutableDictionary dictionaryWithObjectsAndKeys:a, @"a", nil]];
}
How can I do this? 我怎样才能做到这一点?
I don't really understand but I'll try to help you. 我真的不明白,但我会尽力帮助你。
If you want to store Integers into a NSArray use this code to set and get them : 如果要将Integers存储到NSArray中,请使用此代码来设置和获取它们:
// Set
NSArray *yourArray = [[NSArray alloc] initWithObjects:[NSNumber numberWithInt:1], [NSNumber numberWithInt:2], [NSNumber numberWithInt:3],nil];
// Get
int currentNumber;
for(int i=0; i<[yourArray count]; i++)
{
currentNumber = [((NSNumber*)[yourArray objectAtIndex:i]) intValue];
}
So you start with this array, whose content is made of NSString
objects (@"0.1" is a string): 所以你从这个数组开始,其内容由NSString
对象组成(@“0.1”是一个字符串):
NSArray * numberArray = [[NSArray alloc] initWithObjects:@"0.1",@"0.2",@"0.5",nil];
the you can scan each element using this loop: 您可以使用此循环扫描每个元素:
for(NSString *strNum in numberArray) {
float number = [strNum floatValue];
NSLog(@"Number: %f",float);
}
the string to float conversion is possible thanks to the floatValue()
method of NSString
. 由于NSString
的floatValue()
方法,可以进行浮点转换。
You can use doubleValue, longLongValue, integerValue for other types. 您可以将doubleValue,longLongValue,integerValue用于其他类型。 These methods will return 0 if no valid number representation is given with the string. 如果没有给出字符串的有效数字表示,这些方法将返回0。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.