Objective-C 我可以简写 += 增量属性吗?

发布于 2024-12-12 09:34:00 字数 360 浏览 2 评论 0原文

可能的重复:
混合 C 前/后增量/减量与 Objective-C 点运算符一起工作吗?

我正在调试我编写的一段代码。在其中,我使用简写来增加类的属性,

 objectiveCClass.declaredProperty+= 1;

这种增量合法吗?

Possible Duplicate:
Mixing C pre/post increment/decrement with Objective-C dot operator works?

I'm debugging a piece of code that I wrote. Within it, I'm using a shorthand to increment a property of a class

 objectiveCClass.declaredProperty+= 1;

is this kind of increment legal?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(2

德意的啸 2024-12-19 09:34:00

它有效

此代码:

MyObj *m = [[MyObj alloc] init];
m.foo += 1;
NSLog(@"%d", m.foo);

m.foo += 2;
NSLog(@"%d", m.foo);

输出:
1
3

foo 是一个合成的 int 属性:

@property (assign) int foo;

我相信它会做

[m setFoo:([m foo] +1)];

It works

This code:

MyObj *m = [[MyObj alloc] init];
m.foo += 1;
NSLog(@"%d", m.foo);

m.foo += 2;
NSLog(@"%d", m.foo);

Outputs:
1
3

foo is a synthesized int property:

@property (assign) int foo;

I believe it will do

[m setFoo:([m foo] +1)];
半边脸i 2024-12-19 09:34:00
self.declaredProperty += 1;

是简写

[self setDeclaredProperty:self.declaredProperty + 1];
self.declaredProperty += 1;

is shorthand for

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