NSString from int - and maintaining true value?

Discussion in 'Mac Programming' started by Buschmaster, Jun 16, 2008.

  1. Buschmaster
    Expand Collapse
    macrumors 65816

    Buschmaster

    Joined:
    Feb 12, 2006
    Location:
    Minnesota
    #1
    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:
    Code:
    [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?
     
  2. Nutter
    Expand Collapse
    macrumors 6502

    Joined:
    Mar 31, 2005
    Location:
    London, England
    #2
    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.
     
  3. Buschmaster
    Expand Collapse
    thread starter macrumors 65816

    Buschmaster

    Joined:
    Feb 12, 2006
    Location:
    Minnesota
    #3
    Changed it a bit, same problem...

    Code:
    int aInt = 0;
    and

    then
    Code:
    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.
     
  4. therevolution
    Expand Collapse
    macrumors 6502

    Joined:
    May 12, 2003
    #4
    Try this...

    Code:
    NSString *myString = [[NSNumber  numberWithInt:totalInt] stringValue];
    
     
  5. Buschmaster
    Expand Collapse
    thread starter macrumors 65816

    Buschmaster

    Joined:
    Feb 12, 2006
    Location:
    Minnesota
    #5
    Same thing... But thanks for trying!
     
  6. therevolution
    Expand Collapse
    macrumors 6502

    Joined:
    May 12, 2003
    #6
    Wait a sec... Looking at it again:

    Code:
    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:

    Code:
    aInt = [(NSString *)[eachDictionary objectForKey:@"aKey"] intValue];
    
     
  7. Buschmaster
    Expand Collapse
    thread starter macrumors 65816

    Buschmaster

    Joined:
    Feb 12, 2006
    Location:
    Minnesota
    #7
    Oh wow, now I feel dumb.

    Thanks so much for the help! It worked perfectly!
     

Share This Page