Passing Variables?

Discussion in 'Mac Programming' started by Darkroom, Oct 17, 2008.

  1. Darkroom Guest

    Darkroom

    Joined:
    Dec 15, 2006
    Location:
    Montréal, Canada
    #1
    on my quest to be a smarter developer, i'm trying to learn how to remove as much redundant code as i possibly can.

    i have several background colors the user may choose from... the code below only includes 2 (gray and white)... essentially i'm trying to pass the NSColor variable of each IBAction to the setNewDefaults method... unfortunately for me this code isn't working, as my variables are being passed as arguments (?) i though they would be...

    Code:
    - (IBAction)backgroundGray:(id)sender
    	{
    	NSColor *backgroundColor = [NSColor grayColor]; 
    	[self setBackgroundColors:[NSArray arrayWithObjects:backgroundColor, nil]];
    [COLOR="Green"]//	NSData *swatchBackgroundColorOBJECT = [NSKeyedArchiver archivedDataWithRootObject:backgroundColor];
    //	NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
    //	[defaults setObject:swatchBackgroundColorOBJECT forKey:swatchBackgroundColorKEY];[/COLOR]
    	setNewDefaults(backgroundColor);
    	}
    
    - (IBAction)backgroundWhite:(id)sender
    	{
    	NSColor *backgroundColor = [NSColor whiteColor]; 
    	[self setBackgroundColors:[NSArray arrayWithObjects:backgroundColor, nil]];
    [COLOR="Green"]//	NSData *swatchBackgroundColorOBJECT = [NSKeyedArchiver archivedDataWithRootObject:backgroundColor];
    //	NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
    //	[defaults setObject:swatchBackgroundColorOBJECT forKey:swatchBackgroundColorKEY];[/COLOR]
    	setNewDefaults(backgroundColor);
    	}
    	
    - (void)setNewDefaults
    	{
    	NSData *swatchBackgroundColorOBJECT = [NSKeyedArchiver archivedDataWithRootObject:backgroundColor];
    	NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
    	[defaults setObject:swatchBackgroundColorOBJECT forKey:swatchBackgroundColorKEY];
    	}
    
    thoughts?
     
  2. gnasher729 macrumors P6

    gnasher729

    Joined:
    Nov 25, 2005
    #2
    What does the compiler think about it?
     
  3. kainjow Moderator emeritus

    kainjow

    Joined:
    Jun 15, 2000
    #3
    I think you're having another brain fart again ;)

    First, you're calling setNewDefaults as a function, but it's a method. Second, you're passing an argument to it when it doesn't take any arguments. Third, remove the archiver and use the argument (once you fix the method).
     
  4. sord macrumors 6502

    Joined:
    Jun 16, 2004
    #4
    If setNewDefaults was setup like a C method, and you declared the parameter in that method, it would work.

    Take a look at http://www.otierney.net/objective-c.html
    To go quickly to what you are looking for, go to the second instance of "@implementation Fraction"
    Note that you don't need to check if self exists unless you are in the init method.
     

Share This Page