IOS自定义NSDictionary键值类
2016-08-09 18:06
288 查看
使用OC自定义了一个IntPair类作为NSDictionary类的键值,与JAVA中的Pair<int,int>使用方式类似,然而在使用过程中遇到了各种问题,有必要记录一下。
首先,需要实现NSCoping协议,如果不实现的话,在使用IntPair作为key向dictionary中添加数据时会报警告:Sending 'IntPair *__strong to parameter of incompatible type 'id<NSCopying> _Nonnull'
一开始没管,结果运行的时候,在setObject forKey函数这里直接崩溃,setObject forKey的声明如下:
所以应该实现这个方法。
然后,实现了NSCopying协议后,发现key值不能比较是否相等,同样的key值被添加了多次,经过搜索知道,还需要重载NSObject的两个方法:
isEqual方法比较好理解,用于判断两个对象是否相等,比较好实现,
hash方法有必要说明一下,这个方法返回一个整数值作为哈稀表的表地址,熟悉哈稀算法的话就应该明白了,同一个键值的hash返回值也应该相同
下面是IntPair类的完整实现:
IntPair.h
IntPair.m
首先,需要实现NSCoping协议,如果不实现的话,在使用IntPair作为key向dictionary中添加数据时会报警告:Sending 'IntPair *__strong to parameter of incompatible type 'id<NSCopying> _Nonnull'
一开始没管,结果运行的时候,在setObject forKey函数这里直接崩溃,setObject forKey的声明如下:
- (void)setObject:(id)anObject forKey:(id <NSCopying>)aKey;
所以应该实现这个方法。
然后,实现了NSCopying协议后,发现key值不能比较是否相等,同样的key值被添加了多次,经过搜索知道,还需要重载NSObject的两个方法:
-(BOOL) isEqual:(id) object; -(NSUInteger) hash;
isEqual方法比较好理解,用于判断两个对象是否相等,比较好实现,
hash方法有必要说明一下,这个方法返回一个整数值作为哈稀表的表地址,熟悉哈稀算法的话就应该明白了,同一个键值的hash返回值也应该相同
下面是IntPair类的完整实现:
IntPair.h
#import <Foundation/Foundation.h> @interface IntPair : NSObject<NSCoding,NSCopying> @property(nonatomic,assign) int first; @property(nonatomic,assign) int second; -(IntPair *) initWithFirst:(int) first andSecond:(int) second; @end
IntPair.m
#import "IntPair.h" @implementation IntPair -(IntPair *) initWithFirst:(int)first andSecond:(int)second{ self.first = first; self.second = second; return self; } -(BOOL) isEqual:(id)object{ IntPair *pair = (IntPair *)object; if(pair != nil){ if(self.first == pair.first && self.second == pair.second){ return YES; } } return NO; } -(NSUInteger) hash{ return self.first * 1000 + self.second; } -(void) encodeWithCoder:(NSCoder *)aCoder{ NSNumber *first = [[NSNumber alloc] initWithInt:self.first]; NSNumber *second = [[NSNumber alloc] initWithInt:self.second]; [aCoder encodeObject:first forKey:@"first"]; [aCoder encodeObject:second forKey:@"second"]; } -(id) initWithCoder:(NSCoder *)aDecoder{ NSNumber *first = [aDecoder decodeObjectForKey:@"first"]; NSNumber *second = [aDecoder decodeObjectForKey:@"second"]; self.first = [first intValue]; self.second = [second intValue]; return self; } -(id) copyWithZone:(NSZone *)zone{ IntPair *pair = [[IntPair allocWithZone:zone] initWithFirst:self.first andSecond:self.second]; return pair; } @end
相关文章推荐
- IOS开发UI篇之──自定义UIActionSheet
- iOS上自定义URL Schemes
- IOS 5 自定义导航条背景图片
- 自定义的iOS的滚轮选择器,附带农历选择器实现
- 自定义 模式对话框 ios iphone ipad (模仿UIAlertView)
- android ios 在浏览器中自定义URL
- 【iOS-Cocos2d游戏开发之二十一 】自定义精灵类并为你的精灵设置攻击帧(指定开始帧)以及扩展Cocos2d源码的CCAnimation简化动画创建!
- 自定义的iOS的滚轮选择器,附带农历选择器实现
- 自定义的iOS的滚轮选择器,附带农历选择器实现
- 在Android中自定义IOS风格的按钮
- ios 状态栏 覆盖自定义View
- iOS自定义Tab选择
- Unity3D 游戏引擎之IOS自定义游戏摇杆与飞机平滑的移动(十一)
- iOS开发之自定义View
- [iOS]自定义 TableViewCell 实现圆角/渐变色 TableView
- [iOS]实现了一套自定义动画库
- IOS开发UI篇之──自定义UIActionSheet
- 【iOS-Cocos2d游戏开发之二十一 】自定义精灵类并为你的精灵设置攻击帧以及动画创建!【一】
- 自定义checkbox in iOS
- 由ios学到的C++用户自定义转换