• Open Menu Close Menu
  • Apple
  • Shopping Bag
  • Apple
  • Mac
  • iPad
  • iPhone
  • Watch
  • TV
  • Music
  • Support
  • Search apple.com
  • Shopping Bag
 

Lists

Open Menu Close Menu
  • Terms and Conditions
  • Lists hosted on this site
  • Email the Postmaster
  • Tips for posting to public mailing lists
Re: setState has no effect on an NSButton
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: setState has no effect on an NSButton


  • Subject: Re: setState has no effect on an NSButton
  • From: Graham Cox <email@hidden>
  • Date: Tue, 10 May 2011 14:55:39 +1000

On 10/05/2011, at 11:19 AM, Martin Batholdy wrote:

> 	BOOL st = [prefs boolForKey:@"optionA"];
> 	if(st == YES)		 { [buttonA setState:NSOnState]; }
> 	else if(st == NO) 	 { [buttonA setState:NSOffState]; }


Apart from the advice already received, you can reduce this code to:

[buttonA setState:[prefs boolForKey:@"foo"]? NSOnState : NSOffState];

or even

[buttonA setState:[prefs boolForKey:@"foo"]];

if you are prepared to accept that NSOnState and NSOffState are 1 and 0 respectively (which they are, in fact, and unlikely ever to change, but if you're paranoid, the first line fixes that).

Why bother? Because it's more readable than all those if/else constructions, and hence, less prone to bugs. Also, if( st == YES)...else if(st == NO) is redundant - a BOOL can only be YES or NO. The optimiser might optimise away the second comparison anyway, but why be windy in the first place?

--Graham


_______________________________________________

Cocoa-dev mailing list (email@hidden)

Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com

Help/Unsubscribe/Update your Subscription:

This email sent to email@hidden

  • Follow-Ups:
    • Re: setState has no effect on an NSButton
      • From: David Duncan <email@hidden>
References: 
 >setState has no effect on an NSButton (From: Martin Batholdy <email@hidden>)

  • Prev by Date: Re: setState has no effect on an NSButton
  • Next by Date: Re: NSControl Subclass and First Responder
  • Previous by thread: Re: setState has no effect on an NSButton
  • Next by thread: Re: setState has no effect on an NSButton
  • Index(es):
    • Date
    • Thread