2014-09-13 2 views
0

button1과 button2의 2 개의 버튼이 있습니다. 터치 된 해당 버튼에 대해 각각 NSSet을 만들고 버튼 2를 터치하면 set1 값을 표시하고 싶습니다. 버튼 1을 누르면 1이 설정되고 버튼 2를 누르면 설정 2 만 인쇄됩니다. 버튼 2를 눌렀을 때 표시/사용할 수 있도록 button1 액션에서 만든 세트를 어떻게 유지할 수 있습니까? 구현에서 내 간단한 코드새로 생성 된 NSSet 유지하기

에 보라 내가이 :

- (IBAction)button1:(UIButton *)sender { 

    //somecode 

    selectionButton1 = [[NSMutableArray alloc ] initWithObjects:@0,@1,@1,@4,@6,@11, nil]; 

    NSMutableSet *set1 = [NSMutableSet setWithArray: selectionButton1]; 
    NSLog(@"selectionButton1 = %@", set1); 
    NSLog(@"selectionButton2 = %@", set2); 
} 


- (IBAction)button2:(UIButton *) sender { 

    //somecode 

    selectionButton2 = [[NSMutableArray alloc ] initWithObjects:@0,@5,@6,@7,@8,@10, nil]; 
    NSMutableSet *set2 = [NSMutableSet setWithArray: selectionButton2]; 
    NSLog(@"selectionButton1 = %@", set1); 
    NSLog(@"selectionButton2 = %@", set2); 
} 

답변

1

이 세트의 속성을 확인합니다. 다른 클래스에서 액세스 할 필요가 없으면 .m 파일의 private 클래스 확장에서 내부 속성을 만듭니다. 그런 다음 속성에 액세스 self.propertyName를 사용

MyClass.m : 속성에 대한 자세한 내용은

@interface MyClass // Declares a private class extension 

@property (strong, nonatomic) NSMutableSet *set1; 
@property (strong, nonatomic) NSMutableSet *set2 

@end 

@implementation MyClass 

- (IBAction)button1:(UIButton *)sender { 

    //somecode 

    selectionButton1 = [[NSMutableArray alloc ] initWithObjects:@0,@1,@1,@4,@6,@11, nil]; 

    self.set1 = [NSMutableSet setWithArray: selectionButton1]; 
    NSLog(@"selectionButton1 = %@", self.set1); 
    NSLog(@"selectionButton2 = %@", self.set2); 
} 


- (IBAction)button2:(UIButton *) sender { 

    //somecode 

    selectionButton2 = [[NSMutableArray alloc ] initWithObjects:@0,@5,@6,@7,@8,@10, nil]; 
    self.set2 = [NSMutableSet setWithArray: selectionButton2]; 
    NSLog(@"selectionButton1 = %@", self.set1); 
    NSLog(@"selectionButton2 = %@", self.set2); 
} 

@end 

Apple’s documentation를 참조하십시오.

+0

'self.set1 = [NSMutableSet setWithArray : @ [@ 0, @ 1, @ 1, @ 4, @ 6, @ 11]]' – zaph

+1

동의 함. 가능한 한 OP의 코드를 손상시키지 않으려 고 노력했지만,이 방법은 더 깔끔한 방법입니다. –

+0

그래, 나는 질문에 대한 대답에 덧글을 달기 위해 더 열심히 토론했다. – zaph

관련 문제