Question

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.

Was it helpful?

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?

OTHER TIPS

You can set a breakpoint on -[myObject myStringProperty] and see for yourself. If myStringProperty is a getter method that you implement yourself, just click in the left gutter next to the method implementation to set the breakpoint.

If this is a synthesized accessor method, enter it as a symbolic breakpoint in Xcode's breakpoints navigator. Click on the right arrow icon in the navigator section to select the breakpoint navigator, press + at the bottom of the window, and choose "Add Symbolic Breakpoint…". Type -[TheClassName myStringProperty] in the symbol field, then click Done.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top