ARC complaining about instance vairiables

2019-08-23 03:49发布

I am trying to convert non-ARC project to use ARC but for some reason its complaining about the use of all instance variables.

@property (nonatomic,retain)id myvariable;

results in

Error : "Use of undeclared variable _myvariable"

there are some places in my code where I don't want to modify retain count but do an assignment to the property. so I use an instance variable.

adding @syhtnesize myvariable =_myvariable resolves this problem but I am trying to figure out the right way to fix this.

Possible solutions:

1) Add synthesize

2) replace use of instance variable with self.myvariable and make property assigned.

EDIT: Extension of problem ARC errors

3条回答
贪生不怕死
2楼-- · 2019-08-23 03:58

It depends what you need. If you declare property:

@property (strong) NSString *myvariable;

And you want to change name of the private ivar to something different that _myvariable you have to add

@synthesize myvariable = _myVarNewName

but if you want to use exactly the same name with underscore on the front just remove @synthesize (works with iOS 6 and above) and the compiler do the rest so you can access the private variable like _myvariable or public like self.myvariable.

查看更多
SAY GOODBYE
3楼-- · 2019-08-23 04:03

With all the arc stuff

@property (nonatomic, strong) Something *myVariable;

is accessible by both

self.myVariable;

and

_myVariable;

No need to use @synthesize

查看更多
姐就是有狂的资本
4楼-- · 2019-08-23 04:22

It sounds like you've hit a backward compatibility feature.

Since Xcode 4.4 property declarations no longer require an @synthesize statement and without one the compiler auto-generates an _propertyName instance variable.

However with an @synthesize propertyName, as you would pre-Xcode 4.4, then the compiler will auto-generate an propertyName - note no underscore - instance variable.

The compiler messages warning you "Use of undeclared variable _myvariable" suggest you have switched the code to use underscores but still have some @synthesize myvariable statements.

Your use of @synthesize myvariable = _myvariable specifies the name to use for the instance variable directly, and so solves your problem, but removing the @synthesize completely is the usual approach.

查看更多
登录 后发表回答