接口类型不能静态分配?

时间:2022-03-19 01:11:30

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:

Xcode在这一行上给我一个错误:

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?

ARC是否禁止使用未指定的所有权或存储属性合成Objective C对象的属性?

3 个解决方案

#1


66  

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.

在这种情况下,“静态分配”意味着该对象的内存是在编译时分配的。但是,Obj-C中的所有对象都是在运行时分配的,并通过指针访问。

"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).

“接口类型”是一种对编译器有意义的过度技术术语,在这里并不是非常重要。这意味着UIColor表示编译器期望您与变量颜色交互的接口。指向的对象的实际类型可能不同(如类似NSString的类簇)。

#2


6  

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

问题是你只能通过指针访问Objective-C对象,如下所示:

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).

因此,解决方案是在代码中插入星号(您可能打算这样做,而错误只是一个错字)。

#3


5  

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

您需要像这样声明一个UIColor指针并根据您使用的是ARC还是MRR添加retain / strong:

@property (nonatomic, strong) UIColor *color;

#1


66  

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.

在这种情况下,“静态分配”意味着该对象的内存是在编译时分配的。但是,Obj-C中的所有对象都是在运行时分配的,并通过指针访问。

"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).

“接口类型”是一种对编译器有意义的过度技术术语,在这里并不是非常重要。这意味着UIColor表示编译器期望您与变量颜色交互的接口。指向的对象的实际类型可能不同(如类似NSString的类簇)。

#2


6  

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

问题是你只能通过指针访问Objective-C对象,如下所示:

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).

因此,解决方案是在代码中插入星号(您可能打算这样做,而错误只是一个错字)。

#3


5  

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

您需要像这样声明一个UIColor指针并根据您使用的是ARC还是MRR添加retain / strong:

@property (nonatomic, strong) UIColor *color;