1

In this line of code I am reversing a BOOL value:

 someObject.boolValue = ![someObject boolValue];

How can I rewrite this line in "pure" Objective-C syntax, without dot syntax?

jscs
  • 63,694
  • 13
  • 151
  • 195
Olex
  • 1,656
  • 3
  • 20
  • 38
  • 1
    There is nothing wrong with dot syntax. Internally that is converted to the below answers only. Dont hesitate to use that. – iDev Dec 10 '12 at 19:06
  • 1
    Thanks, ACB, I know that. It was only my personal interest – Olex Dec 10 '12 at 19:16
  • 1
    just in case (from AppleDoc): "Dot syntax is purely a convenient wrapper around accessor method calls. When you use dot syntax, the property is still accessed or changed using the getter and setter methods." –  Dec 10 '12 at 20:37

3 Answers3

3

use

[someObject setBoolValue:!([someObject boolValue])];
Boris Prohaska
  • 912
  • 6
  • 16
2

[someObject setBoolValue:![someObject boolValue]];

Jason Whitehorn
  • 13,585
  • 9
  • 54
  • 68
1

Always use NSNumber to avoid your BOOL getting a non-boolean (greater than one) value set to it.

NSNumber * currValue = [NSNumber numberWithBOOL:[someObject boolValue]];
NSNumber * yesNum = [NSNumber numberWithBOOL:YES];
NSNumber * noNum = [NSNumber numberWithBOOL:NO];

if( NSOrderedSame == [currValue compare:yesNum] ){
    [someObject setBoolValue:[noNum boolValue]];
}
else if( NSOrderedSame == [currValue compare:noNum] ){
    [someObject setBoolValue:[yesNum boolValue]];
}
else {
    // Set default
    [someObject setBoolValue:[yesNum boolValue]];
}
Lingus
  • 11
  • 1