Objective-c 中的基本消息计数

发布于 2024-12-29 07:02:15 字数 373 浏览 4 评论 0原文

在以下示例中,有多少消息发送到 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

绮烟 2025-01-05 07:02:15

我只是好奇 Objective-c 可能会在堆栈上缓存 myStringProperty 返回的值。 myStringProperty 返回的值可能会在连续消息之间发生变化,因此缓存可能没有意义。

不,它没有被缓存。每个 objc 消息都会被发送,当然前提是 myObject 不是 nil

编译器不知道方法执行中的任何副作用 (1) 或全局状态的影响 (2)。

  1. 例如,在执行获取 myStringProperty 的过程中,myObject 或其引用的任何内容是否会发生变化?
  2. 例如结果是否受当前时间影响?

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?
彼岸花似海 2025-01-05 07:02:15

您可以在 -[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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文