开发者

(iphone) how to set 'atomic' @property for a variable?

开发者 https://www.devze.com 2023-02-05 05:26 出处:网络
I want to use an integer variable from multiple threads. Hence I want to make getter/setter atomic operation.

I want to use an integer variable from multiple threads.

Hence I want to make getter/setter atomic operation.

at header file

@property (retain) NSNumber* myNumber;

at implementation file

@synthesize myNumber; 

won't compile, what am i missing?

(type of property ‘myNumber’ does not match type of ivar ‘myNumber’)

- edit, I declared it as int myNumber; compilation problem solved

开发者_如何学运维

Also, can I use plain int for this? (not NSNumber* )


In general, you may use int instead of NSNumber. Type choice depends on the problem you are trying to solve.

The particular error message you get says that your instance variable has a different type than the property with the same name. Do you clare your ivar as int myNumber? In this case you should write

@property int myNumber;

Update

Since properties are atomic by default, you do not need to explicitly write:

@property (atomic) int myNumber;


  1. @property (assign) int myNumber;

  2. Add in @interface NSNumber *myNumber;


Check your instance variable type.

Something like this in your header file:

@interface Foo : NSObject {
    NSNumber *myNumber;
}
@property (retain) NSNumber *myNumber;


Use this,

@property (atomic , retain) NSNumber* myNumber;

Or use thislink text it may be helpful

0

精彩评论

暂无评论...
验证码 换一张
取 消