NSHashTable
类似NSSet 、 NSMutableSet
,与其区别在于NSSet 、 NSMutableSet
是对其成员的强类型引用,NSHashTable
可以在创建时设置其对存储对象的持有方式。
特点
-
NSHashTable
是无序的 -
NSHashTable
是可变的 -
NSHashTable
可以持有weak
类型的成员变量 -
NSHashTable
可以在添加成员变量的时候复制成员 -
NSHashTable
可以随意的存储指针并且利用指针的唯一性来进行hash
同一性检查(检查成员变量是否有重复)和对比操作(equal
)
示例
@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
可以在创建时设置其对存储对象的持有方式。
特点
-
NSMapTable
是可变的。 -
NSMapTable
可以通过弱引用来持有keys
和values
,所以当key
或者value
被deallocated
的时候,所存储的实体也会被移除。 -
NSMapTable
可以在添加value
的时候对value
进行复制 - 和
NSHashTable
类似,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
。
特点
- 与
NSMutableArray
一样,使用下标有序的插入或移除元素,且可修改数组内容; - 可以插入或删除
nil
,并且nil
参与count
的计算; -
count
可以set
,如果直接set count
,那么会使用nil
占位; - 可以使用
weak
来修饰成员; - 成员可以是所有指针类型;
- 遵循
NSFastEnumeration
,可以通过for...in
来进行遍历。
示例
- (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)
网友评论