adding NSNumbers in xcode

Discussion in 'iOS Programming' started by ghousemd, Aug 26, 2011.

  1. ghousemd macrumors newbie

    Joined:
    Jun 25, 2011
    #1
    Code:
    for (id key in mTest)//mTest is a NSMutableDictionary
    {
    int result1=0;
    NSMutableArray *myArray=[[NSMutableArray alloc]init];
    
    NSLog(@"%@",key);
    id value = [mTest objectForKey:key];
    id vv=[mFinalDictionary valueForKey:key];
    
    NSInteger value1=[value valueForKey:vv];// VV is a NSNumber
    [myArray addObject:[NSNumber numberWithInt:value1]];// array is storing garbage values
    
    
    result1 +=result1+value1;// here result is not getting added
    NSLog(@"%@",value);
    NSLog(@"%@",vv);
    NSLog(@"%@",value1);
    NSLog(@"%@",result1);
    NSLog(@"%@",myArray);
    
    }
    here i need to add all NSNumbers values for that i am using array to store all values and second option i am using is directly i am trying to add using result operation . any suggestions are welcome
     
  2. robbieduncan Moderator emeritus

    robbieduncan

    Joined:
    Jul 24, 2002
    Location:
    London
    #2
    What does the IDE (XCode) have to do with anything? Anyway you are declaring result1 within the for loop scope. That is almost certainly wrong as a new result1 will be created each time round the loop and initialised to 0. This is a very fundamental error. If you don't mind me asking how much programming training and experience do you have? Do you understand the C rules of variable scope?
     
  3. jiminaus macrumors 65816

    jiminaus

    Joined:
    Dec 16, 2010
    Location:
    Sydney
    #3
    NSInteger is not the same as NSNumber, nor is it a subclass of NSNumber. If you want an NSInteger from an NSNumber sent integerValue to the NSNumber object.

    At the moment, value1 won't contain the number. It will contain the pointer to the NSNumber object returned from valueForKey:. That's the "garbage" values you're seeing in myArray.
     

Share This Page