括号 +点语法问题
我使用 Objective C 已经有几个月了,这是我第一次在 Objective C 中看到这种类型的语法。我想我知道它的等价物,但我想确定一下。
具体来说,代码如下所示
float imageHeight = [player texture].contentSize.height;
'[playertexture].contentSize.height 相当于什么? 我知道点和括号语法可以是同一件事
something.backgroundColor = [UIColor redColor];
[something setBackgroundColor:[UIColor redColor]];
上面的两个几乎是相同的..
那么这就是我的第一个示例中发生的情况吗?我只是从未见过将括号和点语法混合在一起。
谢谢。
I have been working with Objective C for a couple of months now and this has been my first time seeing this type of syntax in objective c. I think I know the equivalent of it but I wanted to be sure.
Specifically, the code looks like this
float imageHeight = [player texture].contentSize.height;
What is the '[player texture].contentSize.height equivalent to?
I know that the dot and bracket syntax can be the same thing
something.backgroundColor = [UIColor redColor];
[something setBackgroundColor:[UIColor redColor]];
The above two are pretty much the same..
So is this what's happening in my first example? I just have never seen a mix of bracket and dot syntax into one.
Thank's.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这可能与以下内容相同:
当点语法应用于对象指针时,它会转换为对适当属性访问器方法的调用。当应用于结构时,它是对该结构的字段之一的直接访问。
因此,假设
[playertexture]
返回一个指向对象(可能是图像)的指针,第一次使用点语法将访问该对象的contentSize
属性。如果-contentSize
也返回一个对象指针,那么下一次使用也将转换为访问器的调用。不过,更有可能的是,contentSize
属性是一个 NSSize,而 NSSize 是一个具有width
和height
字段的结构。因此,点语法的第二次使用很可能会从该结构中检索height
字段。混合点语法的两种不同含义可以很好地获取一个值,但如果您尝试使用它来设置值,则会收到错误。如果我在上一段中的假设是正确的,那么你不能这样做:
This is probably the same thing as:
Dot syntax translates to a call of the appropriate property accessor method when it's applied to object pointers. When applied to structures, it's a direct access of one of the fields of that structure.
So, assuming that
[player texture]
returns a pointer to an object (probably an image), the first use of dot syntax accesses that object'scontentSize
property. If-contentSize
also returns an object pointer, then the next use would also translate to a call of an accessor. More likely, though, thecontentSize
property is a NSSize, and NSSize is a structure withwidth
andheight
fields. So the second use of dot syntax most likely retrieves theheight
field from that structure.Mixing the two different meanings of dot syntax works fine for getting a value, but you'll get an error if you try to use it to set values. If my assumptions in the previous paragraph are correct, you can't do this: