美文网首页
iOS NSHashTable、NSMapTable、NSPoi

iOS NSHashTable、NSMapTable、NSPoi

作者: FieryDragon | 来源:发表于2022-10-04 19:01 被阅读0次

    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可以通过弱引用来持有keysvalues,所以当key或者valuedeallocated的时候,所存储的实体也会被移除。
    • 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)
    
    

    相关文章

      网友评论

          本文标题:iOS NSHashTable、NSMapTable、NSPoi

          本文链接:https://www.haomeiwen.com/subject/hmznwrtx.html