递增属性 int (self.int++)

发布于 2024-09-07 13:06:15 字数 135 浏览 3 评论 0原文

如何增加整数属性?

你不能做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 技术交流群。

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

发布评论

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

评论(2

残龙傲雪 2024-09-14 13:06:15

integer++ 之所以有效,是因为您直接访问 integer 并向 integer 分配新值,而不是发送消息并使用访问器。假设 integer 被声明为 NSInteger 属性,以下语句将对整数的值产生相同的效果,但是直接访问不符合 KVO。

[self setInteger:0];
self.integer = self.integer + 1; // use generated accessors
NSLog(@"Integer is :%d",[self integer]); // Integer is: 1
integer++;
NSLog(@"Integer is :%d",[self integer]); // Integer is: 2

integer++ works because you are accessing the integer directly and assigning a new value to integer instead of sending a message and using accessors. Assuming that integer is declared as an NSInteger property the following statements are will have the same effect on the value of integer, however direct access is not KVO compliant.

[self setInteger:0];
self.integer = self.integer + 1; // use generated accessors
NSLog(@"Integer is :%d",[self integer]); // Integer is: 1
integer++;
NSLog(@"Integer is :%d",[self integer]); // Integer is: 2
身边 2024-09-14 13:06:15

我相信 Obj-C 在过去一两年进行了更新,这样这种代码就可以工作了。我编写了一个快速测试,发现以下代码全部有效且有效:

在我的 .h 中:

#import <Cocoa/Cocoa.h>

@interface TheAppDelegate : NSObject <NSApplicationDelegate> {
    NSUInteger value;
}

@property NSUInteger otherValue;

- (NSUInteger) value;
- (void) setValue:(NSUInteger)value;

@end

在我的 .m 中:

#import "TheAppDelegate.h"

@implementation TheAppDelegate

- (void)applicationDidFinishLaunching:(NSNotification *)aNotification
{
    value = self.otherValue = 1;
    NSLog(@"%lu %lu", (unsigned long)value, (unsigned long)self.value);
    NSLog(@"%lu %lu", (unsigned long)_otherValue, (unsigned long)self.otherValue);
    self.value++;
    self.otherValue++;
    NSLog(@"%lu %lu", (unsigned long)value, (unsigned long)self.value);
    NSLog(@"%lu %lu", (unsigned long)_otherValue, (unsigned long)self.otherValue);
}

- (NSUInteger) value
{
    return value;
}

- (void) setValue:(NSUInteger)_value
{
    value = _value;
}

@end

我的输出:

1 1
1 1
2 2
2 2

我相信我读过的某个地方有一份技术文档解释了这一点,但我不记得在哪里我找到了。我相信它说了这样的话:

x++ will be returned to x+=1

并且 x+=y will be returned with x=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:

#import <Cocoa/Cocoa.h>

@interface TheAppDelegate : NSObject <NSApplicationDelegate> {
    NSUInteger value;
}

@property NSUInteger otherValue;

- (NSUInteger) value;
- (void) setValue:(NSUInteger)value;

@end

In my .m:

#import "TheAppDelegate.h"

@implementation TheAppDelegate

- (void)applicationDidFinishLaunching:(NSNotification *)aNotification
{
    value = self.otherValue = 1;
    NSLog(@"%lu %lu", (unsigned long)value, (unsigned long)self.value);
    NSLog(@"%lu %lu", (unsigned long)_otherValue, (unsigned long)self.otherValue);
    self.value++;
    self.otherValue++;
    NSLog(@"%lu %lu", (unsigned long)value, (unsigned long)self.value);
    NSLog(@"%lu %lu", (unsigned long)_otherValue, (unsigned long)self.otherValue);
}

- (NSUInteger) value
{
    return value;
}

- (void) setValue:(NSUInteger)_value
{
    value = _value;
}

@end

My output:

1 1
1 1
2 2
2 2

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 to x+=1

And that x+=y will be replaced with x=x+y

Further, x=y.a=z will be replaced with y.a=z,x=y.a (for when you're dealing with properties - not structs)

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