Xcode iPhone:Property ... requires method ... to be defined - 使用@syntehsize、@dynamic 或提供方法实现
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/7154759/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Xcode iPhone: Property ... requires method ... to be defined - use @syntehsize, @dynamic or provide a method implementation
提问by Artur
in an .m file I have 4 warnings relating to one command:
在 .m 文件中,我有 4 个与一个命令相关的警告:
@end
@结尾
Property 'myHeader' requires method '-myHeader' to be defined - use @synthesize, @dynamic or provide a method implementation
Property 'customCell' requires the method 'setCustomCell:' to be defined - use @synthesize, @dynamic or provide a method implementation
Property 'customCell' requires method '-customCell' to be defined - use @synthesize, @dynamic or provide a method implementation
Property 'myHeader' requires the method 'setMyHeader' to be defined - use @synthesize, @dynamic or provide a method implementation
属性“myHeader”需要定义方法“-myHeader” - 使用@synthesize、@dynamic 或提供方法实现
属性 'customCell' 需要定义方法 'setCustomCell:' - 使用 @synthesize、@dynamic 或提供方法实现
属性“customCell”需要定义方法“-customCell” - 使用@synthesize、@dynamic 或提供方法实现
属性 'myHeader' 需要定义方法 'setMyHeader' - 使用 @synthesize、@dynamic 或提供方法实现
I browsed through forums but I am hopeless so far - can you explain to me (beginner programmer...) how to debug it? Thanks a million!
我浏览了论坛,但到目前为止我毫无希望 - 你能向我解释(初学者程序员......)如何调试它吗?太感谢了!
回答by lucianf
Resuscitating an old thread. Probably not the OP issue, but I thought it would be worth mentioning this, in case anyone else (like me) runs into this. You get the same message if you're implementing a new property in a class category (even puzzling if you're using LLVM 4 which no longer requires @synthesize). This is possible using the technique outlined here:
复苏旧线程。可能不是 OP 问题,但我认为值得一提的是,以防其他人(如我)遇到这个问题。如果您在类类别中实现新属性,您会收到相同的消息(如果您使用的是不再需要 @synthesize 的 LLVM 4,甚至会令人费解)。使用此处概述的技术可以做到这一点:
@interface SomeClass (Private)
@property (nonatomic, assign) id newProperty;
@end
NSString * const kNewPropertyKey = @"kNewPropertyKey";
@implementation SomeClass (Private)
@dynamic newProperty;
- (void)setNewProperty:(id)aObject
{
objc_setAssociatedObject(self, kNewPropertyKey, aObject, OBJC_ASSOCIATION_ASSIGN);
}
- (id)newProperty
{
return objc_getAssociatedObject(self, kNewPropertyKey);
}
@end
回答by superjessi
It means that you need to synthesize those variables. Synthesizing creates the setter and getter methods for you. To do this, you need to include the following code in your implementation (.m) file:
这意味着您需要综合这些变量。Synthesizing 为您创建了 setter 和 getter 方法。为此,您需要在实现 (.m) 文件中包含以下代码:
@synthesize myHeader;
@synthesize customCell;
Adding these lines should take care of your 4 errors.
添加这些行应该可以解决您的 4 个错误。
You also have the option of defining the setter and getter methods yourself, but unless you want to do something specific, go with @synthesize for now.
您还可以选择自己定义 setter 和 getter 方法,但除非您想做一些特定的事情,否则现在就使用 @synthesize。
回答by psagers
In your class header (the associated .h file), you apparently have something resembling the following:
在您的类标题(相关的 .h 文件)中,您显然有类似以下内容的内容:
@property SomeClass *myHeader;
@property SomeClass *customCell;
This tells the compiler that you want your class to have these properties, but you still have to tell it how to get and set the values on instances. You have three options:
这告诉编译器您希望您的类具有这些属性,但您仍然必须告诉它如何获取和设置实例的值。您有三个选择:
In the @implementation section (in the .m file), you can add
@synthesize myHeader, customCell;
to tell the compiler to automatically generate methods to get and set these properties. This is the easiest and usually what you want.
You can implement the methods yourself like so:
- (SomeClass *)myHeader { // Return the value of myHeader } - (void)setMyHeader:(SomeClass *)inMyHeader { // Set myHeader to inMyHeader }
This is generally used for derived or dynamically generated properties, or when you want to do extra work when they're changed.
You can use
@dynamic myHeader, customCell;
to tell the compiler that implementations will be provided at runtime. This is rarely used.
在@implementation 部分(在 .m 文件中),您可以添加
@synthesize myHeader, customCell;
告诉编译器自动生成获取和设置这些属性的方法。这是最简单的,通常也是您想要的。
您可以像这样自己实现这些方法:
- (SomeClass *)myHeader { // Return the value of myHeader } - (void)setMyHeader:(SomeClass *)inMyHeader { // Set myHeader to inMyHeader }
这通常用于派生或动态生成的属性,或者当您想要在更改它们时做额外的工作时。
您可以使用
@dynamic myHeader, customCell;
来告诉编译器将在运行时提供实现。这很少使用。
回答by Mitch Lindgren
Using @property
tells the compiler that you intend to access the corresponding member variable through "setter" and "getter" functions, i.e. functions which change the value of the variable or return its value. The warnings you're seeing are because you have not defined the required functions. I'm guessing your header (.h) file looks something like this:
Using@property
告诉编译器您打算通过“setter”和“getter”函数访问相应的成员变量,即改变变量值或返回其值的函数。您看到的警告是因为您尚未定义所需的函数。我猜你的头 (.h) 文件看起来像这样:
@property(readwrite, assign) <type> myHeader;
@property(readwrite, assign) <type> customCell;
Because you've told the compiler that those variables should be accessible through setter and getter functions, you need to define those functions. There are two ways of doing this. The easiest is to use @synthesize
, by adding the following to your implementation (.m) file:
因为您已经告诉编译器这些变量应该可以通过 setter 和 getter 函数访问,所以您需要定义这些函数。有两种方法可以做到这一点。最简单的方法是使用@synthesize
,将以下内容添加到您的实现 (.m) 文件中:
@synthesize myHeader;
@synthesize customCell;
The @synthesize
line causes the - myHeader
, - setMyHeader
, - customCell
and - setCustomCell
methods to be generated automatically at compile time. You may also define those methods manually, but doing so is usually unnecessary.
的@synthesize
线引起- myHeader
,- setMyHeader
,- customCell
和- setCustomCell
要在编译时自动生成的方法。您也可以手动定义这些方法,但这样做通常是不必要的。
Here's a page with more information about properties in Objective C.
回答by Flyingdiver
You have two ivars defined in @property statements, but you're missing the matching @synthesize statement. Add the @synthesize to the class definition.
您在 @property 语句中定义了两个 ivars,但缺少匹配的 @synthesize 语句。将@synthesize 添加到类定义中。
回答by morningstar
You have some options, but the simplest, and probably what you want, is to use @synthesize.
您有一些选择,但最简单的,也可能是您想要的,是使用@synthesize。
In your .m file, somewhere after @implementation, add
在您的 .m 文件中,@implementation 之后的某处添加
@synthesize myHeader;
and likewise for the other properties.
其他属性也是如此。
In order for that to work, your class also has to have a member variable called myHeader. You might already have that. If not, in your .h file, somewhere after @interface, put
为了使它起作用,您的类还必须有一个名为 myHeader 的成员变量。你可能已经有了。如果没有,在你的 .h 文件中,@interface 之后的某个地方,把
SomeType *myHeader;
Of course change "SomeType" to the appropriate type that's the same as you have in your property declaration (the thing you have that looks like)
当然,将“SomeType”更改为与您在属性声明中相同的适当类型(您拥有的东西看起来像)
@property (retain) SomeType *myHeader;
回答by jtbandes
You have declared an @property — this is effectively a contract saying you will implement getter and setter methods, but you haven't.You either need to:
你已经声明了一个@property——这实际上是一个约定,你将实现 getter 和 setter 方法,但你没有。您要么需要:
- implement
-foo
and-setFoo:
for each property, or - use @synthesize in your
.m
file and the compiler will do it for you.
- 为每个财产实施
-foo
和-setFoo:
,或 - 在您的
.m
文件中使用 @synthesize,编译器会为您完成。
More info can be found in the documentation here.
可以在此处的文档中找到更多信息。