0

I am learning Objective-C following a book that is three or four years old (called Tap, Move, Shake). The problem I had is in one of the examples in the book they explain how to create a label and button, then they create the outlet in the .h file, like this:

@property (weak, nonatomic) IBOutlet UILabel *label;

Then they just use the variable label in the .m file within a function, like this:

- (void)generate
{
// pick two numbers between 1 and 9
int a = 1 + arc4random() % 9;
int b = 1 + arc4random() % 9;

// calculate the sum
int sum = a + b;

// create one question
label.text = [NSString stringWithFormat: @"%d + %d", a, b];

// save the answer in the tag property of the label
label.tag = sum;
}

But XCode gives me this error: "Must use struct to refer to type 'label'."

To my understanding, the variable has been already defined in the .h as a local variable so I should be able to retrieve if from the .m, right? (or at least this is what I understand from the book examples).

I've managed to fix this by replacing 'label' by 'self.label' - which makes it work fine now.

I would really appreciate if someone can give me a bit more of information on this.

Thanks a lot in advance!

Cris
  • 197
  • 1
  • 4
  • 16
  • `label` is a property, not a local variable. Read up on properties in the Objective-C docs. – rmaddy Jan 22 '15 at 03:29
  • [DrummerB's answer to "Declaration definition of variables locations in ObjC"](http://stackoverflow.com/a/12632467/) is also a good explanation. You might also want to read [CRD's answer to "ARC complaining about instance vairiables"](http://stackoverflow.com/a/21174243/). – jscs Jan 22 '15 at 03:34
  • Many thanks rmaddy and Josh for your help, those entries have been very helpful! Apologies for creating a duplicate – Cris Jan 27 '15 at 04:17

0 Answers0