用@dynamic或@synthesize实现@property有什么区别?


当前回答

@dynamic通常在运行时动态创建属性时使用(如上所述)。NSManagedObject就是这样做的(为什么它的所有属性都是动态的)——这抑制了一些编译器警告。

有关如何动态创建属性(不含NSManagedObject和CoreData:)的良好概述,请参阅:http://developer.apple.com/library/ios/#documentation/Cocoa/Conceptual/ObjCRuntimeGuide/Articles/ocrtDynamicResolution.html#//apple_ref/doc/uid/TP40008048-CH102-SW1

其他回答

有一件事要补充的是,如果一个属性被声明为@dynamic,它将不会占用内存(我与分配工具确认)。结果是,您可以在类category中声明属性。

@dynamic通常在运行时动态创建属性时使用(如上所述)。NSManagedObject就是这样做的(为什么它的所有属性都是动态的)——这抑制了一些编译器警告。

有关如何动态创建属性(不含NSManagedObject和CoreData:)的良好概述,请参阅:http://developer.apple.com/library/ios/#documentation/Cocoa/Conceptual/ObjCRuntimeGuide/Articles/ocrtDynamicResolution.html#//apple_ref/doc/uid/TP40008048-CH102-SW1

下面是@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;
}

根据文件:

https://developer.apple.com/library/mac/documentation/cocoa/conceptual/ObjCRuntimeGuide/Articles/ocrtDynamicResolution.html

@dynamic告诉编译器在运行时提供了访问器方法。

通过一些调查,我发现提供访问器方法会覆盖@dynamic指令。

@synthesize告诉编译器为你创建这些访问器(getter和setter)

@property告诉编译器将创建访问器,并且可以使用点符号或[object message]来访问。

根据苹果文档。

在类的实现块中使用@synthesize语句告诉编译器创建与@property声明中给出的规范匹配的实现。

您可以使用@dynamic语句告诉编译器,如果它无法找到@property声明指定的访问器方法的实现,则取消警告。

更多信息:

https://developer.apple.com/library/ios/documentation/General/Conceptual/DevPedia-CocoaCore/DeclaredProperty.html