gpt4 book ai didi

ios - 保留我新创建的 NSSet

转载 作者:行者123 更新时间:2023-11-29 02:40:56 25 4
gpt4 key购买 nike

我有 2 个按钮:button1 和 button2。我想为每个触摸的相应按钮创建一个 NSSet,并希望在触摸按钮 2 时显示 set1 值,反之亦然。按下按钮 1 时仅打印 set 1,按下按钮 2 时仅打印 set2。我如何保留在 button1 操作中创建的集合,以便在按下按钮 2 时可以显示/使用它。看看我的简单代码

在实现中我有:

- (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);
}

最佳答案

为你的集合创建属性。如果您不需要从其他类访问它们,请在您的 .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 .

关于ios - 保留我新创建的 NSSet,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25819625/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com