View Full Version : NSString from int - and maintaining true value?

Jun 16, 2008, 06:29 PM
I have a UILabel that has text which will come in from an int. It comes in in this way because it is inside of a for loop where it keeps gaining more and more values. Here's how I'm setting the dictionary's value:
[myPlate setValue:[NSString stringWithFormat:@"%d",aInt] forKey:@"aKey"];

What happens here is that the string will come out but it becomes a huge number, like an identifier.

And if I leave off the string with format I get a warning and it causes the iPhone simulator to crash.

How can I maintain the ACTUAL value of the int and pass it into an NSString to be placed into a dictionary?

Jun 16, 2008, 07:15 PM
What's the exact declaration of your aInt variable? Are you sure it's an int?

It may also help to post the for loop.

Jun 16, 2008, 07:18 PM
Changed it a bit, same problem...

int aInt = 0;


for(eachDictionary in theArray) {
aInt = (int)[eachDictionary objectForKey:@"aKey"];

totalInt += aInt;

NSString *myString = [NSString stringWithFormat:@"%d",totalInt];

[myDictionary setValue:myString forKey:@"aKey"];

With totalInt being a property.

Jun 16, 2008, 11:04 PM
Try this...

NSString *myString = [[NSNumber numberWithInt:totalInt] stringValue];

Jun 16, 2008, 11:13 PM
Try this...

NSString *myString = [[NSNumber numberWithInt:totalInt] stringValue];

Same thing... But thanks for trying!

Jun 16, 2008, 11:40 PM
Wait a sec... Looking at it again:

aInt = (int)[eachDictionary objectForKey:@"aKey"];

That doesn't look right. You're pulling an object pointer and casting it to int - almost certainly this is why you're seeing big numbers.

Do you know what those objects should be? If they're strings:

aInt = [(NSString *)[eachDictionary objectForKey:@"aKey"] intValue];

Jun 17, 2008, 08:15 AM
Oh wow, now I feel dumb.

Thanks so much for the help! It worked perfectly!