48

I tried to put this in the header file of my view object:

@property (nonatomic) UIColor color;

to store the color that lines should be drawn with in this view.

Xcode gives me an error on this line:

Interface type cannot be statically allocated

What does that mean, and what should I do?

EDIT:

I did add a *, and at the point of synthesis, it said:

ARC forbid synthesizing a property of Objective C object with unspecified ownership or storage attribute?

William Sham
  • 12,849
  • 11
  • 50
  • 67
  • The error message in your edit is a completely different issue. Please create a new question for that. – jscs Dec 10 '11 at 23:00

3 Answers3

88

Your variable is for an object type, and as such must be declared as a pointer:

@property (nonatomic) UIColor * color;    // Note the asterisk

"Statically allocated" in this case would mean that the memory for that object was allocated at compile-time. All objects in Obj-C, however, are allocated at runtime and accessed through pointers.

"Interface type" is kind of an overly-technical term that's meaningful to the compiler, and not terribly important here. It means that UIColor represents the interface through which the compiler expects you to interact with the variable color. The actual type of the object pointed to may be different (as with a class cluster like NSString).

jscs
  • 63,694
  • 13
  • 151
  • 195
  • 1
    why can I declare CGFloat statically then? – William Sham Dec 10 '11 at 22:23
  • The size of an Objective-C object is unknown at compile time, right? –  Dec 10 '11 at 22:24
  • 1
    @William Sham CGFloat is not an Objective-C object. –  Dec 10 '11 at 22:25
  • 3
    @William: Because `CGFloat` is not an object, but a primitive. – jscs Dec 10 '11 at 22:25
  • @WTP: I can't remember. I think the size of a generic object (`struct objc_object`?) is known. With non-fragile base classes, I think that all objects are the same size, with another chunk of memory for ivars. – jscs Dec 10 '11 at 22:28
  • @WTP: Greg Parker [explains some of it](http://www.sealiesoftware.com/blog/archive/2009/01/27/objc_explain_Non-fragile_ivars.html). – jscs Dec 10 '11 at 22:33
  • Of course, not quite _all_ objects are allocated at runtime. Constant NSStrings and Protocols are statically allocated. Blocks are weird. –  Dec 11 '11 at 11:19
6

The problem is that you can only access Objective-C objects by reference through pointers, like this:

UIColor *color;

you can't have a "bare" object, like this:

UIColor color;

So the solution is to insert the asterisk in your code (which you probably meant to do, and the bug is just a typo).

6

You need to declare a UIColor pointer like so and add retain/strong depending on whether you're using ARC or MRR:

@property (nonatomic, strong) UIColor *color;
Reed Olsen
  • 9,099
  • 4
  • 37
  • 47