Search code examples
objective-cmultithreadingcachingmessageobjc-message-send

Basic message counting in Objective-c


In the following example how many messages are sent to myObject?

- (void) myMethod:(id) myObject
    NSLog(@"%@", myObject.myStringProperty);
    NSLog(@"%@", myObject.myStringProperty);
    NSLog(@"%@", myObject.myStringProperty);
}

I'm just curious about Objective-c potentially caching the value returned by myStringProperty on the stack. The value returned by myStringProperty could change between successive messages so perhaps caching doesn't make sense.


Solution

  • Three

    I'm just curious about Objective-c potentially caching the value returned by myStringProperty on the stack. The value returned by myStringProperty could change between successive messages so perhaps caching doesn't make sense.

    Nope, it's not cached. Every objc message is sent, provided of course myObject is not nil.

    The compiler has no idea about any side effects within the method's execution (1) or influence of the global state (2).

    1. e.g. does myObject or anything it references ever change during the execution of getting myStringProperty?
    2. e.g. is the result affected by the current time?