Performance difference between dot notation versus method call in Objective-C

前端 未结 5 2045
执笔经年
执笔经年 2020-12-15 09:21

You can use a standard dot notation or a method call in Objective-C to access a property of an object in Objective-C.

myObject.property = YES;
5条回答
  •  时光说笑
    2020-12-15 10:04

    Dot notation for property access in Objective-C is a message send, just as bracket notation. That is, given this:

    @interface Foo : NSObject
    @property BOOL bar;
    @end
    
    Foo *foo = [[Foo alloc] init];
    foo.bar = YES;
    [foo setBar:YES];
    

    The last two lines will compile exactly the same. The only thing that changes this is if a property has a getter and/or setter attribute specified; however, all it does is change what message gets sent, not whether a message is sent:

    @interface MyView : NSView
    @property(getter=isEmpty) BOOL empty;
    @end
    
    if ([someView isEmpty]) { /* ... */ }
    if (someView.empty) { /* ... */ }
    

    Both of the last two lines will compile identically.

提交回复
热议问题