2010-06-25 5 views
2

어떻게 정수 속성을 증가시킬 수 있습니까?증가 속성 int (self.int ++)

당신은 self.integer++을 할 수 없어,

마지막은 "정수"의 값을 유지합니다 .. 당신은 integer++을 수행 할 수 있습니다,하지만 난 아니에요 확실 거세한 숫양 여부 그게 유지할 것인가?

감사합니다.

답변

3

integer++은 메시지를 보내고 접근자를 사용하는 대신 integer에 직접 액세스하고 integer에 새 값을 할당하기 때문에 작동합니다. integerNSInteger 속성으로 선언된다고 가정하면 다음 문은 정수 값에 동일한 효과를 갖지만 직접 액세스는 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 
+1

감사합니다. [blahblahblah,이 주석을 채우려 고 시도합니다. – Emil

0

Obj-C가 지난 1 년 또는 2 년 동안 업데이트되어 이러한 종류의 코드가 작동한다고 생각합니다. 나는 빠른 테스트를 쓴 다음과 같은 코드가 모두 유효하고 작동 발견 : 내하는 .m에서

#import <Cocoa/Cocoa.h> 

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

@property NSUInteger otherValue; 

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

@end 

:

#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 

내 출력 :

내 .H에서

1 1 
1 1 
2 2 
2 2 

내가 읽은 기술 문서가 있는데 그 이유를 설명하는 곳이 기억이 나지 않습니다. 당신이있을 때를 위해 y.a=z,x=y.a (와, x=y.a=z이 대체됩니다

x++x+=1

로 변경 얻을 것이다 그리고 그 x+=y는 또한 x=x+y

로 대체됩니다 : 나는 그것의 라인을 따라 뭔가 말했다 믿는다 속성을 다루는 - 구조체가 아님)

관련 문제