递增属性 int (self.int++)
如何增加整数属性?
你不能做self.integer++
,你可以做integer++
,但我不确定这是否会保留它。
最后一个会保留该值吗? “整数”?
谢谢。
How can you increment a integer property?
You can't do self.integer++
, you can do integer++
, but I'm not sure wether or not that will retain it..
Will the last one retain the value of "integer"?
Thanks.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
integer++
之所以有效,是因为您直接访问integer
并向integer
分配新值,而不是发送消息并使用访问器。假设integer
被声明为NSInteger
属性,以下语句将对整数的值产生相同的效果,但是直接访问不符合 KVO。integer++
works because you are accessing theinteger
directly and assigning a new value tointeger
instead of sending a message and using accessors. Assuming thatinteger
is declared as anNSInteger
property the following statements are will have the same effect on the value of integer, however direct access is not KVO compliant.我相信 Obj-C 在过去一两年进行了更新,这样这种代码就可以工作了。我编写了一个快速测试,发现以下代码全部有效且有效:
在我的 .h 中:
在我的 .m 中:
我的输出:
我相信我读过的某个地方有一份技术文档解释了这一点,但我不记得在哪里我找到了。我相信它说了这样的话:
x++
will be returned tox+=1
并且
x+=y
will be returned withx=x+y
此外,
x=ya=z
将替换为ya=z,x=ya
(当您处理属性 - 不是结构)I believe Obj-C was updated in the last year or two so that this kind of code works. I wrote a quick test and found the following code is all valid and works:
in my .h:
In my .m:
My output:
I believe there's a technical document someplace that I read that explains this, but I can't remember where I found it. I believe it said something along the lines of:
x++
will get changed tox+=1
And that
x+=y
will be replaced withx=x+y
Further,
x=y.a=z
will be replaced withy.a=z,x=y.a
(for when you're dealing with properties - not structs)