简体   繁体   中英

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

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 this link text it may be helpful

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM