質問

I'm using Xcode 4.5 on a brand new installation of Mountain Lion. Compiler is LLVM4.1 and my project is using ARC. (I've dabbled with Xcode in the past, but not touched it for a while).

My question:

I understand that in newer versions of Xcode it is no longer necessary to include the @synthesize directive (and sure enough, when I ctrl-drag from a UI component in the .xib to the .h file the directives don't get created in the .m file), yet without them I am unable to access those properties, and instead get the "Unknown receiver xxx" error.

Simple example:

I created a Single View iPhone project with a label ('myLabel') and a button ('myButton'). I ctrl-dragged from the UILabel in ViewController.xib to ViewController.h to create the following property:

@property (strong, nonatomic) IBOutlet UILabel *myLabel;

and I created the button action by dragging and dropping too.

In the .m file I wrote the following code in the myButton method:

- (IBAction)myButton:(id)sender {
  NSLog(@"The label text is %@", [myLabel text]);
}

Result:

This gives me a compile time error ("Unknown receiver myLabel").

I can still access my property either using an underscore (i.e. [_myLabel text]) or by adding "@synthesize myLabel;" at the top of the .m file manually, but I thought I didn't need to do this, or that it should be done for me. Can anyone explain what I am doing wrong in the code (or perhaps if this is due to some setting I need to tweak?)

Thanks

p.s. I note that my .m file now also has its own @interface block at the top (presumably for declaring private vars?)

正しい解決策はありません

他のヒント

Automatic ivars are prefixed with an underscore to differentiate them from property names. So you will need to use _myLabel. If you don't like the default prefix, you can change the ivar name for the property using @synthesize

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top