2

I have an class, and in the header I define this:

@interface MyViewController : UIViewController {
    BOOL blackBackground;
}
@property(nonatomic, assign) BOOL blackBackground;

In the implementation I have the @synthesize for blackBackground.

Then, I instantiate this object and do:

[myViewController setBlackBackground:YES];

now that boolean should have the value YES (or true ;) ). But then, I check:

if ([myViewController blackBackground]) {
  NSLog(@"yep, it's true");
}

however, it doesn't seem to respond or return anything, either the value doesn't get set or I can't call/check it. Any idea what's wrong there?

Krishna Raj Salim
  • 7,331
  • 5
  • 34
  • 66
Thanks
  • 40,109
  • 71
  • 208
  • 322

3 Answers3

17

Are you sure you are actually using the above code? It has syntax errors and will not compile, so it is entirely possible you are not seeing that code executed because you are running old copies of the code.

The issue that you cannot declare a scalar property to have a any sort of retain,assign,copy semantic, it will result in a compile error. You should change:

@property(nonatomic, assign) BOOL blackBackground;

to

@property(nonatomic) BOOL blackBackground;
Louis Gerbarg
  • 43,356
  • 8
  • 80
  • 90
  • in fact it was a stupid typo in my code! Thanks! Strange thing is, I got it to work with @property(nonatomic, assign). what's the difference when I don't provide assign or retain (just nothing)? How do the created accessors look? – Thanks Jul 20 '09 at 12:28
  • 3
    retain means that the when you set the property you set it to the actual object (so your object will see mutations that occur), and bump the retain count. copy means your set the property to a copy of the object passed in. assign means you direct assignment (like in the retain) without increasing the retainCount. Since scalar values do not accept messages or participate in ObjC retain/release those attributes do not apply to them. – Louis Gerbarg Jul 20 '09 at 13:07
  • Not sure this is all correct: "You typically use this attribute for scalar types such as NSInteger and CGRect, or (in a reference-counted environment) for objects you don’t own, such as delegates." – typeoneerror Aug 25 '11 at 04:11
  • http://stackoverflow.com/questions/6837334/objective-c-defaults-to-atomic-for-scalar-properties/6837570#6837570 – typeoneerror Aug 25 '11 at 04:13
1

I think it is isBlackBackground or of course you can use self.blackBackground

John
  • 2,012
  • 2
  • 21
  • 33
1

I agree with @Louis Gerbarg and believe you had some typo, or just built older version of the code, but I disagree with him about the reason he provided for your error: He wrote:

The issue that you cannot declare a scalar property to have a any sort of retain,assign,copy semantic, it will result in a compile error. You should change:

@property(nonatomic, assign) BOOL blackBackground;

to

@property(nonatomic) BOOL blackBackground;

And according to the attached link and Apple's docs:

@property (nonatomic, assign) BOOL blackBackground;

And:

@property (nonatomic) BOOL blackBackground;

Are the same, since "assign" is the default behavior for properties.

(Right?)

previous similar stackOverflow question and answer

Community
  • 1
  • 1
ofirbt
  • 1,846
  • 7
  • 26
  • 41