Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

Darkroom

Guest
Original poster
Dec 15, 2006
2,445
0
Montréal, Canada
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?
 

kainjow

Moderator emeritus
Jun 15, 2000
7,958
7
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).
 

sord

macrumors 6502
Jun 16, 2004
352
0
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.
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.