How to add two NSNumber objects?

Discussion in 'iOS Programming' started by Skov, Feb 14, 2011.

  1. Skov, Feb 14, 2011
    Last edited by a moderator: Feb 14, 2011

    Skov macrumors newbie

    Joined:
    Feb 13, 2011
    #1
    Hey

    i have a problem :-(

    i have a simple tableView based app, i´m using core-data in my project.

    i pub. the cell with a "name" and a number at subtitle, and i have made a button in the cell. the plan is that when the user push the button it will add the number from the "subtitle" to a NSNumber in my core data model, and if the value >= to X then it need to open a view.

    i have don this, but nothing happens :-(

    Code:
    ToDo *toDo;
    		
    		int a = [toDo.pointsValue floatValue];
    		int b = [toDo.totalPoint floatValue];
    		 
    		int c = a + b;
    		
    		toDo.totalPoint = [NSNumber numberWithInteger:c];
    		
    		if ([toDo.totalPoint floatValue] > [toDo.goodiesPoints floatValue]){
    			
    		CustomPickerViewController *addToDoView = [[CustomPickerViewController alloc] initWithNibName:@"CustomPickerViewController" bundle:[NSBundle mainBundle]];
    		
    		UINavigationController *navController = [[UINavigationController alloc] initWithRootViewController: addToDoView];
    		[self.navigationController presentModalViewController:navController animated:YES];
    
    hope u can help me out.
    Skov
     
  2. ianray macrumors 6502

    Joined:
    Jun 22, 2010
    Location:
    @
    #2
    NSNumber is a class which holds a representation of numeric types (including integers and floats) for the purpose of storage or transmission.

    In your specific case I would suggest using the debugger, or placing NSLog statements before the conditional test, in order to understand the code paths.

    Code:
    // gcc -W -Wall -framework Foundation nsnumber.m
    #import <Foundation/Foundation.h>
    
    int main(void) {
    
    	NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
    
    	NSNumber* numberX = [NSNumber numberWithFloat:0.1];
    	NSNumber* numberY = [NSNumber numberWithFloat:0.2];
    	float     z       = [numberX floatValue] + [numberY floatValue];
    	NSNumber* numberZ = [NSNumber numberWithFloat:z];
    
    	NSLog(@"numberX:%f numberY:%f z:%f,%f", [numberX floatValue], [numberY floatValue], z, [numberZ floatValue]);
    
    	[pool drain];
    	return 0;
    }
    
     
  3. admanimal macrumors 68040

    Joined:
    Apr 22, 2005
    #3
    You're also mixing ints and floats pretty carelessly...it might not be the cause of your problem but it's ugly.
     
  4. Skov, Feb 19, 2011
    Last edited by a moderator: Feb 19, 2011

    Skov thread starter macrumors newbie

    Joined:
    Feb 13, 2011
    #4
    updte

    did this after my fetch req.:


    Code:
            NSNumber *a = toDoo.pointsValue;
    	
    	NSNumber *b = toDoo.totalPoint;
    	
    	CGFloat x = [a floatValue] + [b floatValue];
    	
    	
    	//Note: it is better to use CGFloat rather than plain float
    	CGFloat y = [toDoo.goodiesPoints floatValue];
    	if (x >= y)
    	{		
    					
    		[self performSelector:@selector(winView) 
    					withObject:nil 
    					afterDelay:0.5];
    
    its like im missing something to make this work like i want:

    if pointValue in tha cell is 10, the ideer is that u need to push that 5 times if goodiesPoints is set to 50. so it like im missing a way to set totalPoints to the new value ( x ) can u help me out ?
     

Share This Page