How to convert NSArray to integer values

On my iPhone, using the code below, I can get the value "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 a value from an array, as in the 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?

+3
source share
3 answers

Perhaps this is what you are looking for.

iphone using an array to determine in the range of the main graph

Hope this helps you.

+1
source

I really do not understand, but I will try to help you.

If you want to store integers in NSArray, use this code to set and get them:

// 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];
}
+6
source

, , NSString (@ "0.1" - ):

NSArray * numberArray = [[NSArray alloc] initWithObjects:@"0.1",@"0.2",@"0.5",nil];

, :

for(NSString *strNum in numberArray) {
  float number = [strNum floatValue];
  NSLog(@"Number: %f",float);
}

Converting a string to float is possible thanks to the method floatValue() NSString.

You can use doubleValue, longLongValue, integerValue for other types. These methods will return 0 if a valid numeric representation with a string is not specified.

+1
source

Source: https://habr.com/ru/post/1789510/


All Articles