iOS NSHashTable、NSMapTable、NSPoi

2022-10-04  本文已影响0人  FieryDragon

NSHashTable

类似NSSet 、 NSMutableSet,与其区别在于NSSet 、 NSMutableSet是对其成员的强类型引用,NSHashTable可以在创建时设置其对存储对象的持有方式。

特点
示例
@interface ViewController ()
@property (nonatomic, strong)NSHashTable *hashTable;
@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.view.backgroundColor = [UIColor whiteColor];
    [self hashTableTest];

}
- (void)hashTableTest {
    self.hashTable = [NSHashTable hashTableWithOptions:NSPointerFunctionsWeakMemory];
    NSObject *obj1 = [[NSObject alloc] init];
    NSObject *obj2 = [[NSObject alloc] init];
    NSObject *obj3 = [[NSObject alloc] init];
    NSObject *obj4 = [[NSObject alloc] init];
    NSObject *obj5 = obj4;
    [self.hashTable addObject:obj1];
    [self.hashTable addObject:obj2];
    [self.hashTable addObject:obj3];
    [self.hashTable addObject:obj4];
    [self.hashTable addObject:obj5];
    [self.hashTable addObject:obj1];

    NSLog(@"Members: %@", [self.hashTable allObjects]);
    
    
    UIButton *btn1 = [UIButton buttonWithType:UIButtonTypeCustom];
    btn1.backgroundColor = [UIColor redColor];
    btn1.frame = CGRectMake(100, 100, 100, 100);
    [btn1 addTarget:self action:@selector(btn1Click) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:btn1];
}
   
- (void)btn1Click {
    NSLog(@"Members: %@", [self.hashTable allObjects]);
}

@end

结果:

Members: (
    "<NSObject: 0x6000025346c0>",
    "<NSObject: 0x6000025346b0>",
    "<NSObject: 0x6000025346d0>",
    "<NSObject: 0x6000025346e0>"
)

Members: (
)

NSMapTable

类似NSDictionary 、NSMutableDictionary,区别于NSDictionary 、 NSMutableDictionary会复制keys并且通过强引用values来实现存储,NSMapTable可以在创建时设置其对存储对象的持有方式。

特点
示例
- (void)mapTableTest {
    self.mapTable = [[NSMapTable alloc] initWithKeyOptions:NSPointerFunctionsCopyIn valueOptions:NSPointerFunctionsWeakMemory capacity:10];
    NSObject *obj1 = [[NSObject alloc] init];
    NSObject *obj2 = [[NSObject alloc] init];
    NSObject *obj3 = [[NSObject alloc] init];
    NSObject *obj4 = [[NSObject alloc] init];
    NSObject *obj5 = obj4;
    
    [self.mapTable setObject:obj1 forKey:@"obj1"];
    [self.mapTable setObject:obj2 forKey:@"obj2"];
    [self.mapTable setObject:obj3 forKey:@"obj3"];
    [self.mapTable setObject:obj4 forKey:@"obj4"];
    [self.mapTable setObject:obj5 forKey:@"obj5"];
    
    NSLog(@"Members: %@", self.mapTable);
    
    UIButton *btn2 = [UIButton buttonWithType:UIButtonTypeCustom];
    btn2.backgroundColor = [UIColor redColor];
    btn2.frame = CGRectMake(100, 100, 100, 100);
    [btn2 addTarget:self action:@selector(btn2Click) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:btn2];
    
}
- (void)btn2Click {
    NSLog(@"Members: %@", self.mapTable);
}

结果

Members: NSMapTable {
[7] obj5 -> <NSObject: 0x600003a3c3d0>
[9] obj2 -> <NSObject: 0x600003a3c3a0>
[11] obj4 -> <NSObject: 0x600003a3c3d0>
[12] obj3 -> <NSObject: 0x600003a3c3c0>
[13] obj1 -> <NSObject: 0x600003a3c390>
}
Members: NSMapTable {
}

NSPointerArray

类似于数组的集合,但具有更广泛的可用内存语义;继承自NSObject

特点
示例
- (void)pointerArrayTest {
    self.pointerArray = [[NSPointerArray alloc] initWithOptions:NSPointerFunctionsWeakMemory];
    NSObject *obj1 = [[NSObject alloc] init];
    NSObject *obj2 = [[NSObject alloc] init];
    NSObject *obj3 = [[NSObject alloc] init];
    NSObject *obj4 = [[NSObject alloc] init];
    NSObject *obj5 = obj4;
    [self.pointerArray insertPointer:(__bridge void * _Nullable)obj1 atIndex:0];
    [self.pointerArray insertPointer:(__bridge void * _Nullable)obj2 atIndex:1];
    [self.pointerArray insertPointer:(__bridge void * _Nullable)obj3 atIndex:2];
    [self.pointerArray insertPointer:(__bridge void * _Nullable)obj4 atIndex:3];
    [self.pointerArray insertPointer:(__bridge void * _Nullable)obj5 atIndex:4];


    NSLog(@"Members: %@", [self.pointerArray allObjects]);
    for (NSObject *objc in self.pointerArray) {
        NSLog(@"Member: %@", objc);
    }
    
    self.pointerArray.count = 10;
    NSLog(@"Members: %@", [self.pointerArray allObjects]);
    for (NSObject *objc in self.pointerArray) {
        NSLog(@"Member: %@", objc);
    }
    
    UIButton *btn3 = [UIButton buttonWithType:UIButtonTypeCustom];
    btn3.backgroundColor = [UIColor redColor];
    btn3.frame = CGRectMake(100, 100, 100, 100);
    [btn3 addTarget:self action:@selector(btn3Click) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:btn3];
}
- (void)btn3Click {
    NSLog(@"Members: %@", [self.pointerArray allObjects]);
    for (NSObject *objc in self.pointerArray) {
        NSLog(@"Member: %@", objc);
    }
}

结果:

Members: (
    "<NSObject: 0x60000012c220>",
    "<NSObject: 0x60000012c230>",
    "<NSObject: 0x60000012c250>",
    "<NSObject: 0x60000012c260>",
    "<NSObject: 0x60000012c260>"
)
Member: <NSObject: 0x60000012c220>
Member: <NSObject: 0x60000012c230>
Member: <NSObject: 0x60000012c250>
Member: <NSObject: 0x60000012c260>
Member: <NSObject: 0x60000012c260>

Members: (
    "<NSObject: 0x60000012c220>",
    "<NSObject: 0x60000012c230>",
    "<NSObject: 0x60000012c250>",
    "<NSObject: 0x60000012c260>",
    "<NSObject: 0x60000012c260>"
)
Member: <NSObject: 0x60000012c220>
Member: <NSObject: 0x60000012c230>
Member: <NSObject: 0x60000012c250>
Member: <NSObject: 0x60000012c260>
Member: <NSObject: 0x60000012c260>
Member: (null)
Member: (null)
Member: (null)
Member: (null)
Member: (null)

Members: (
)
Member: (null)
Member: (null)
Member: (null)
Member: (null)
Member: (null)
Member: (null)
Member: (null)
Member: (null)
Member: (null)
Member: (null)

上一篇下一篇

猜你喜欢

热点阅读