XCode 4.4 Auto @Synthesize failing on an XCode 4.3

2019-04-09 23:13发布

问题:

It's as simple as that.

Running Lion.

  1. I just upgraded to XCode 4.4
  2. loaded my most recent XCode 4.3 project file
  3. commented out one @synthesize line of code
  4. and errors abound. :(

Verified compiler is set to 'LLVM 4.0'.

Then I did the same test but created a new project within XCode 4.4, and voila! Auto @synthesize works within a 4.4 project.

Auto @synthesize also seems to work on new properties added to the code. But existing old one generate an error.

Anyone else experience this? Any other things I should check for?

I really want the auto generation features to work.

Thanks.

回答1:

The Error isn't the way you declare the property but in the way that you use it.

Auto-synthesized properties create a backing store with a leading underscore by default.

So in your code when you have a property declared as:

@property (nonatomic, strong) UILabel *sectorLabel;

and you auto-sythesize - something like this is being auto-generated for you by the compiler:

@synthesize sectorLabel = _sectorLabel;

Now you can access it through the the property:

self.sectorLabel;

Or, you can access the backing store directly with:

_sectorLabel;


回答2:

Solved it!

So this is what I did.

ViewController.h

@interface ViewController : UIViewController
// Public:

@property (nonatomic, strong) UILabel *sectorLabel;

@end

ViewController.m

@implementation ViewController

//@synthesize sectorLabel;

And then this error popped up.

ViewController.m:48:2: Use of undeclared identifier 'sectorLabel'; did you mean '_sectorLabel'?

It resolved the moment I changed the code to:

self.sectorLabel

XCode 4.3 compiled and worked fine without the need for having the 'self.' keyword. But XCode 4.4 seems to have gotten more strict about it.



回答3:

The problem with the answer accepted is that you just changed the behavior of your code by adding the self.property.

Accessing the ivar versus the property are two different things. The correct answer would be to change the code to use the ivar correctly by adding the underscore.

This is not about strictness its about a change to the default property synthesization for xcode. Also, in most style guides you will read that Apple discourages _ivar names so I guess thats out the window.

In my code I only use self. when I INTEND to access through the property getter and setter. Not changing my existing code for this behavior its a waste of time.