Objective-c 中的基本消息计数
在以下示例中,有多少消息发送到 myObject
?
- (void) myMethod:(id) myObject
NSLog(@"%@", myObject.myStringProperty);
NSLog(@"%@", myObject.myStringProperty);
NSLog(@"%@", myObject.myStringProperty);
}
我只是好奇 Objective-c 可能会在堆栈上缓存 myStringProperty
返回的值。 myStringProperty
返回的值可能会在连续消息之间发生变化,因此缓存可能没有意义。
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
三
不,它没有被缓存。每个 objc 消息都会被发送,当然前提是
myObject
不是nil
。编译器不知道方法执行中的任何副作用 (1) 或全局状态的影响 (2)。
Three
Nope, it's not cached. Every objc message is sent, provided of course
myObject
is notnil
.The compiler has no idea about any side effects within the method's execution (1) or influence of the global state (2).
myStringProperty
?您可以在 -[myObject myStringProperty] 上设置断点并亲自查看。如果 myStringProperty 是您自己实现的 getter 方法,只需单击方法实现旁边的左侧装订线即可设置断点。
如果这是一个综合访问器方法,请将其作为符号断点输入 Xcode 的断点导航器中。单击导航器部分中的右箭头图标以选择断点导航器,按窗口底部的 +,然后选择“添加符号断点...”。在符号字段中键入 -[TheClassName myStringProperty],然后单击“完成”。
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.