objective-c @synthesize vs @dynamic,有什么区别?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1160498/
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
@synthesize vs @dynamic, what are the differences?
提问by nico
What are the differences between implementing a @propertywith @dynamicor @synthesize?
实施@propertywith@dynamic或之间有什么区别@synthesize?
回答by diederikh
@synthesize will generate getter and setter methods for your property. @dynamic just tells the compiler that the getter and setter methods are implemented not by the class itself but somewhere else (like the superclass or will be provided at runtime).
@synthesize 将为您的属性生成 getter 和 setter 方法。@dynamic 只是告诉编译器 getter 和 setter 方法不是由类本身实现的,而是其他地方(如超类或将在运行时提供)。
Uses for @dynamic are e.g. with subclasses of NSManagedObject(CoreData) or when you want to create an outlet for a property defined by a superclass that was not defined as an outlet.
@dynamic 的使用例如与NSManagedObject(CoreData) 的子类一起使用,或者当您想要为由未定义为插座的超类定义的属性创建插座时。
@dynamic also can be used to delegate the responsibility of implementing the accessors. If you implement the accessors yourself within the class then you normally do not use @dynamic.
@dynamic 也可用于委派实现访问器的责任。如果您在类中自己实现访问器,那么您通常不使用@dynamic。
Super class:
超级班:
@property (nonatomic, retain) NSButton *someButton;
...
@synthesize someButton;
Subclass:
子类:
@property (nonatomic, retain) IBOutlet NSButton *someButton;
...
@dynamic someButton;
回答by Alex Rozanski
Take a look at this article; under the heading "Methods provided at runtime":
看看这篇文章;在“运行时提供的方法”标题下:
Some accessors are created dynamically at runtime, such as certain ones used in CoreData's NSManagedObject class. If you want to declare and use properties for these cases, but want to avoid warnings about methods missing at compile time, you can use the @dynamic directive instead of @synthesize.
...
Using the @dynamic directive essentially tells the compiler "don't worry about it, a method is on the way."
一些访问器是在运行时动态创建的,例如 CoreData 的 NSManagedObject 类中使用的某些访问器。如果您想为这些情况声明和使用属性,但又想避免在编译时出现有关方法丢失的警告,您可以使用@dynamic 指令而不是@synthesize。
...
使用@dynamic 指令本质上是告诉编译器“别担心,方法已经在路上了”。
The @synthesizedirective, on the other hand, generates the accessor methods for you at compile time (although as noted in the "Mixing Synthesized and Custom Accessors" section it is flexible and does not generate methods for you if either are implemented).
@synthesize另一方面,该指令在编译时为您生成访问器方法(尽管如“混合合成和自定义访问器”部分所述,它很灵活,如果实现了任何一个,都不会为您生成方法)。
回答by philsquared
As others have said, in general you use @synthesize to have the compiler generate the getters and/ or settings for you, and @dynamic if you are going to write them yourself.
正如其他人所说,通常您使用@synthesize 让编译器为您生成getter 和/或设置,如果您要自己编写它们,则使用@dynamic。
There is another subtlety not yet mentioned: @synthesize willlet you provide an implementation yourself, of either a getter or a setter. This is useful if you only want to implement the getter for some extra logic, but let the compiler generate the setter (which, for objects, is usually a bit more complex to write yourself).
还有一个尚未提及的微妙之处:@synthesize将让您自己提供一个 getter 或 setter 实现。如果您只想为一些额外的逻辑实现 getter,但让编译器生成 setter(对于对象,通常自己编写更复杂一些),这很有用。
However, if you do write an implementation for a @synthesize'd accessor it must still be backed by a real field (e.g., if you write -(int) getFoo();you must have an int foo;field). If the value is being produce by something else (e.g. calculated from other fields) then you have to use @dynamic.
但是,如果您确实为@synthesize 的访问器编写了一个实现,它仍然必须由一个真实的字段支持(例如,如果您编写,则-(int) getFoo();必须有一个int foo;字段)。如果该值是由其他东西产生的(例如从其他字段计算),那么您必须使用@dynamic。
回答by mifortin
@dynamic is typically used (as has been said above) when a property is being dynamically created at runtime. NSManagedObject does this (why all its properties are dynamic) -- which suppresses some compiler warnings.
@dynamic 通常在运行时动态创建属性时使用(如上所述)。NSManagedObject 这样做(为什么它的所有属性都是动态的)——这会抑制一些编译器警告。
For a good overview on how to create properties dynamically (without NSManagedObject and CoreData:, see: http://developer.apple.com/library/ios/#documentation/Cocoa/Conceptual/ObjCRuntimeGuide/Articles/ocrtDynamicResolution.html#//apple_ref/doc/uid/TP40008048-CH102-SW1
有关如何动态创建属性的概述(没有 NSManagedObject 和 CoreData:,请参阅:http: //developer.apple.com/library/ios/#documentation/Cocoa/Conceptual/ObjCRuntimeGuide/Articles/ocrtDynamicResolution.html#// apple_ref/doc/uid/TP40008048-CH102-SW1
回答by mirror
here is example of @dynamic
这是@dynamic的例子
#import <Foundation/Foundation.h>
@interface Book : NSObject
{
NSMutableDictionary *data;
}
@property (retain) NSString *title;
@property (retain) NSString *author;
@end
@implementation Book
@dynamic title, author;
- (id)init
{
if ((self = [super init])) {
data = [[NSMutableDictionary alloc] init];
[data setObject:@"Tom Sawyer" forKey:@"title"];
[data setObject:@"Mark Twain" forKey:@"author"];
}
return self;
}
- (void)dealloc
{
[data release];
[super dealloc];
}
- (NSMethodSignature *)methodSignatureForSelector:(SEL)selector
{
NSString *sel = NSStringFromSelector(selector);
if ([sel rangeOfString:@"set"].location == 0) {
return [NSMethodSignature signatureWithObjCTypes:"v@:@"];
} else {
return [NSMethodSignature signatureWithObjCTypes:"@@:"];
}
}
- (void)forwardInvocation:(NSInvocation *)invocation
{
NSString *key = NSStringFromSelector([invocation selector]);
if ([key rangeOfString:@"set"].location == 0) {
key = [[key substringWithRange:NSMakeRange(3, [key length]-4)] lowercaseString];
NSString *obj;
[invocation getArgument:&obj atIndex:2];
[data setObject:obj forKey:key];
} else {
NSString *obj = [data objectForKey:key];
[invocation setReturnValue:&obj];
}
}
@end
int main(int argc, char **argv)
{
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
Book *book = [[Book alloc] init];
printf("%s is written by %s\n", [book.title UTF8String], [book.author UTF8String]);
book.title = @"1984";
book.author = @"George Orwell";
printf("%s is written by %s\n", [book.title UTF8String], [book.author UTF8String]);
[book release];
[pool release];
return 0;
}
回答by user1447414
As per the documentation:
根据文档:
@dynamic tells the compiler that the accessor methods are provided at runtime.
@dynamic 告诉编译器访问器方法是在运行时提供的。
With a little bit of investigation I found out that providing accessor methods override the @dynamic directive.
通过一些调查,我发现提供访问器方法会覆盖 @dynamic 指令。
@synthesize tells the compiler to create those accessors for you (getter and setter)
@synthesize 告诉编译器为您创建这些访问器(getter 和 setter)
@property tells the compiler that the accessors will be created, and that can be accessed with the dot notation or [object message]
@property 告诉编译器将创建访问器,并且可以使用点符号或 [object message] 访问它
回答by Yingpei Zeng
One thing want to add is that if a property is declared as @dynamic it will not occupy memory (I confirmed with allocation instrument). A consequence is that you can declare property in class category.
要补充的一件事是,如果一个属性被声明为@dynamic,它不会占用内存(我用分配工具确认)。结果是您可以在类类别中声明属性。
回答by arango_86
As per the Apple documentation.
根据苹果文档。
You use the @synthesizestatement in a class's implementation block to tell the compiler to create implementations that match the specification you gave in the @propertydeclaration.
您@synthesize在类的实现块中使用语句告诉编译器创建与您在@property声明中给出的规范相匹配的实现。
You use the @dynamicstatement to tell the compiler to suppress a warning if it can't find an implementation of accessor methods specified by an @propertydeclaration.
@dynamic如果找不到@property声明指定的访问器方法的实现,您可以使用该语句告诉编译器取消警告。
More info:-
更多信息:-

