3

I am new to Objective-C and trying to make my way through a book. I was following along a pretty basic tutorial, when I came upon an error. The code is afaik the same as in the book.

I have a class with MyController.h and MyController.m. The error happens as during the declaration.

MyController.h:

#import <Foundation/Foundation.h>
@interface MyController : NSObject
@property (assign) IBOutlet id *textLabel;

- (IBAction)clickMeButtonClicked:(id)sender;
- (IBAction)deleteMeButtonClicked:(id)sender;

@end    

The third line gives me a warning and an error:

The error:

Pointer to non-const type 'id' with no explicit ownership

The warning:

Property with 'iboutlet' attribute must be an object type (invalid '__strong id *')

As I said I am just beginning to learn and I am surely missing something very obvious here. It seems to have something to do with the 'id' type, but according to the book, there is nothing wrong.

Thank you for your help!

max
  • 115
  • 1
  • 9
  • The redundant * was in fact the problem. Apparently the author missed that mistake. Thanks to everyone who pointed this out. – max Dec 05 '12 at 15:36
  • I have just done some additional research and the solution that allows you to continue in the tutorial is going for `@property (assign) IBOutlet NSTextField *textLabel;` – max Dec 05 '12 at 17:20

4 Answers4

3

id is already a pointer:

typedef struct objc_object {
     Class isa;
} *id;

Therefore you don't need * in the declaration. Try:

@property (assign) IBOutlet id textLabel;
trojanfoe
  • 120,358
  • 21
  • 212
  • 242
  • Just curious, where did you find the definition for the `id` type? I tried using Xcode's go to definition but it didn't want to do anything – Dan F Dec 05 '12 at 15:40
  • I am not at my Mac, so I used Google ;-) This might help: http://stackoverflow.com/questions/1990695/in-cocoa-how-is-the-id-type-defined – trojanfoe Dec 05 '12 at 15:42
0

id does not need to be declared as a pointer. The correct way to declare it would be as follows:

@property (assign) IBOutlet id textLabel;//Don't need the *

You can find a more comprehensive explanation of the id type in this question

Community
  • 1
  • 1
Dan F
  • 17,654
  • 5
  • 72
  • 110
  • Could you be a little bit more specific? What would be the correct way to do it? – max Dec 05 '12 at 15:30
0

Assign should probably be changed to strong, and remove the * before textLabel.

emillime
  • 531
  • 6
  • 21
0

Try changing (assign) into (nonatomic, retain), since this is an IBOutlet and not a primitive, you have to retain and release it. It is also good practice to put nonatomic there, it has something to do with threads but I cannot give you exact info about that. Additionally do not forget to put [textLabel release] in the dealloc method, since you need to release every object you retained, created or copied.

Yunus Nedim Mehel
  • 12,089
  • 4
  • 50
  • 56