Osman
Osman

Reputation: 1771

NSMutableArray to NSNumber

I have an array that has:

12, "+", 56, "+", "", "(", 56, "+", 65, ")"

now let us say i want to add

[NSMutableArray objectAtIndex: 1] + [NSMutableArray objectAtIndex: 3]

i know i need to convert the array parts to NSNumber, but i can not find anything that says how to do that if thats possible. Also i should add that the numbers are put in as NSString and to change that will be very painful and can be done as a last resort.

Upvotes: 1

Views: 1833

Answers (2)

janusfidel
janusfidel

Reputation: 8106

you can do it like

NSNumber *number = (NSNumber*)[NSMutableArray objectAtIndex:1];

Upvotes: 1

danielbeard
danielbeard

Reputation: 9149

You can get a nsnumber from a string like so:

NSString *str = @"222";
NSNumber *num = [NSNumber numberWithInt:[str intValue]];

So to convert from an array to an nsnumber:

[NSNumber numberWithInt:[[NSMutableArray objectAtIndex: 1] intValue]];

But keep in mind that you can't directly add two nsnumber objects together, it would have to be something like this:

NSNumber *sum = [NSNumber numberWithInt:([one intValue] + [two intValue])];

And if you end up using float values, you can just replace intValue with floatValue

Upvotes: 2

Related Questions