swift Objective-C Dictionary/Number 的一個(gè)有意思的區(qū)別

swift Objective-C Dictionary 的一個(gè)有意思的區(qū)別

上代碼

swift


func setKeyValue(map: inout [AnyHashable : String], key: AnyHashable, value: String) {
    print("swift: set key:\(key) value:\(value)")
    map[key] = value
    print("swift: \(map)")
}
func testDictioary() {
    
    var map: [AnyHashable : String] = [:]
    
    setKeyValue(map: &map, key: true, value: "true")
    setKeyValue(map: &map, key: false, value: "false")
    setKeyValue(map: &map, key: UInt32(0), value: "UInt32(0)")
    setKeyValue(map: &map, key: UInt32(1), value: "UInt32(1)")
    setKeyValue(map: &map, key: UInt64(0), value: "UInt64(0)")
    setKeyValue(map: &map, key: UInt64(1), value: "UInt64(1)")
    setKeyValue(map: &map, key: Int32(0), value: "Int32(0)")
    setKeyValue(map: &map, key: Int32(1), value: "Int32(1)")
    setKeyValue(map: &map, key: Int64(0), value: "Int64(0)")
    setKeyValue(map: &map, key: Int64(1), value: "Int64(1)")

    setKeyValue(map: &map, key: Float32(0), value: "Float32(0)")
    setKeyValue(map: &map, key: Float32(1), value: "Float32(1)")
    
    setKeyValue(map: &map, key: Float64(0), value: "Float64(0)")
    setKeyValue(map: &map, key: Float64(1), value: "Float64(1)")

    print("swift: result \(map)")
    
    let objcDictionary = map as NSDictionary
    print("swift: result as NSDictionary \(objcDictionary)")
    OC.printDictioary(map)
}

objc

// .h
@interface OC : NSObject

+ (void)testDictioary;

+ (void)printDictioary:(NSDictionary *)dict;

@end

//.m
static void setKeyVulue(NSMutableDictionary * dict, id key, id value) {
    NSLog(@"oc: set key:%@ %@ value:%@", [key class], key, value);
    dict[key] = value;
    NSLog(@"oc:%@", dict);
}

@implementation OC

+ (void)testDictioary {
    NSMutableDictionary * dict = [NSMutableDictionary dictionary];
    
    setKeyVulue(dict, @(true), @"@true");
    setKeyVulue(dict, @(false), @"@false");
    setKeyVulue(dict, [NSNumber numberWithBool:true], @"true");
    setKeyVulue(dict, [NSNumber numberWithBool:false], @"false");
    
    setKeyVulue(dict, [NSNumber numberWithUnsignedInt:0], @"UInt32(0)");
    setKeyVulue(dict, [NSNumber numberWithUnsignedInt:1], @"UInt32(1)");

    setKeyVulue(dict, [NSNumber numberWithUnsignedLong:0], @"UInt64(0)");
    setKeyVulue(dict, [NSNumber numberWithUnsignedLong:1], @"UInt64(1)");
    
    setKeyVulue(dict, [NSNumber numberWithInt:0], @"Int32(0)");
    setKeyVulue(dict, [NSNumber numberWithInt:1], @"Int32(1)");
    
    setKeyVulue(dict, [NSNumber numberWithLong:0], @"Int64(0)");
    setKeyVulue(dict, [NSNumber numberWithLong:1], @"Int64(1)");
    

    setKeyVulue(dict, [NSNumber numberWithFloat:0], @"Float32(0)");
    setKeyVulue(dict, [NSNumber numberWithFloat:1], @"Float32(1)");

    setKeyVulue(dict, [NSNumber numberWithDouble:0], @"Float64(0)");
    setKeyVulue(dict, [NSNumber numberWithDouble:1], @"Float64(1)");
    
    NSLog(@"oc: result: %@", dict);
}
+ (void)printDictioary:(NSDictionary *)dict {
    [dict enumerateKeysAndObjectsUsingBlock:^(id  _Nonnull key, id  _Nonnull obj, BOOL * _Nonnull stop) {
        NSLog(@"%@ %@: %@", [(NSObject *)key class], key, obj);
    }];
}

@end

log

swift

swift: set key:true value:true
swift: [AnyHashable(true): "true"]
swift: set key:false value:false
swift: [AnyHashable(true): "true", AnyHashable(false): "false"]
swift: set key:0 value:UInt32(0)
swift: [AnyHashable(0): "UInt32(0)", AnyHashable(true): "true", AnyHashable(false): "false"]
swift: set key:1 value:UInt32(1)
swift: [AnyHashable(false): "false", AnyHashable(1): "UInt32(1)", AnyHashable(0): "UInt32(0)", AnyHashable(true): "true"]
swift: set key:0 value:UInt64(0)
swift: [AnyHashable(false): "false", AnyHashable(1): "UInt32(1)", AnyHashable(0): "UInt64(0)", AnyHashable(true): "true"]
swift: set key:1 value:UInt64(1)
swift: [AnyHashable(false): "false", AnyHashable(1): "UInt64(1)", AnyHashable(0): "UInt64(0)", AnyHashable(true): "true"]
swift: set key:0 value:Int32(0)
swift: [AnyHashable(false): "false", AnyHashable(1): "UInt64(1)", AnyHashable(0): "Int32(0)", AnyHashable(true): "true"]
swift: set key:1 value:Int32(1)
swift: [AnyHashable(false): "false", AnyHashable(1): "Int32(1)", AnyHashable(0): "Int32(0)", AnyHashable(true): "true"]
swift: set key:0 value:Int64(0)
swift: [AnyHashable(false): "false", AnyHashable(1): "Int32(1)", AnyHashable(0): "Int64(0)", AnyHashable(true): "true"]
swift: set key:1 value:Int64(1)
swift: [AnyHashable(false): "false", AnyHashable(1): "Int64(1)", AnyHashable(0): "Int64(0)", AnyHashable(true): "true"]
swift: set key:0.0 value:Float32(0)
swift: [AnyHashable(false): "false", AnyHashable(1): "Int64(1)", AnyHashable(0): "Float32(0)", AnyHashable(true): "true"]
swift: set key:1.0 value:Float32(1)
swift: [AnyHashable(false): "false", AnyHashable(1): "Float32(1)", AnyHashable(0): "Float32(0)", AnyHashable(true): "true"]
swift: set key:0.0 value:Float64(0)
swift: [AnyHashable(false): "false", AnyHashable(1): "Float32(1)", AnyHashable(0): "Float64(0)", AnyHashable(true): "true"]
swift: set key:1.0 value:Float64(1)
swift: [AnyHashable(false): "false", AnyHashable(1): "Float64(1)", AnyHashable(0): "Float64(0)", AnyHashable(true): "true"]
swift: result [AnyHashable(false): "false", AnyHashable(1): "Float64(1)", AnyHashable(0): "Float64(0)", AnyHashable(true): "true"]
swift: result as NSDictionary {
    0 = false;
    1 = "Float64(1)";
    0 = "Float64(0)";
    1 = true;
}
2021-06-25 13:57:28.915145+0800 TS[40462:1220674] __NSCFBoolean 1: true
2021-06-25 13:57:28.915668+0800 TS[40462:1220674] __NSCFNumber 0: Float64(0)
2021-06-25 13:57:28.915758+0800 TS[40462:1220674] __NSCFBoolean 0: false
2021-06-25 13:57:28.915841+0800 TS[40462:1220674] __NSCFNumber 1: Float64(1)

objc


2021-06-25 14:05:31.264627+0800 TS[40537:1225701] oc: set key:__NSCFNumber 1 value:@true
2021-06-25 14:05:31.264755+0800 TS[40537:1225701] oc:{
    1 = "@true";
}
2021-06-25 14:05:31.264794+0800 TS[40537:1225701] oc: set key:__NSCFNumber 0 value:@false
2021-06-25 14:05:31.264855+0800 TS[40537:1225701] oc:{
    0 = "@false";
    1 = "@true";
}
2021-06-25 14:05:31.264887+0800 TS[40537:1225701] oc: set key:__NSCFBoolean 1 value:true
2021-06-25 14:05:31.264939+0800 TS[40537:1225701] oc:{
    0 = "@false";
    1 = true;
}
2021-06-25 14:05:31.264968+0800 TS[40537:1225701] oc: set key:__NSCFBoolean 0 value:false
2021-06-25 14:05:31.265003+0800 TS[40537:1225701] oc:{
    0 = false;
    1 = true;
}
2021-06-25 14:05:31.265034+0800 TS[40537:1225701] oc: set key:__NSCFNumber 0 value:UInt32(0)
2021-06-25 14:05:31.283843+0800 TS[40537:1225701] oc:{
    0 = "UInt32(0)";
    1 = true;
}
2021-06-25 14:05:31.283919+0800 TS[40537:1225701] oc: set key:__NSCFNumber 1 value:UInt32(1)
2021-06-25 14:05:31.284109+0800 TS[40537:1225701] oc:{
    0 = "UInt32(0)";
    1 = "UInt32(1)";
}
2021-06-25 14:05:31.284183+0800 TS[40537:1225701] oc: set key:__NSCFNumber 0 value:UInt64(0)
2021-06-25 14:05:31.284317+0800 TS[40537:1225701] oc:{
    0 = "UInt64(0)";
    1 = "UInt32(1)";
}
2021-06-25 14:05:31.284393+0800 TS[40537:1225701] oc: set key:__NSCFNumber 1 value:UInt64(1)
2021-06-25 14:05:31.284498+0800 TS[40537:1225701] oc:{
    0 = "UInt64(0)";
    1 = "UInt64(1)";
}
2021-06-25 14:05:31.284581+0800 TS[40537:1225701] oc: set key:__NSCFNumber 0 value:Int32(0)
2021-06-25 14:05:31.284658+0800 TS[40537:1225701] oc:{
    0 = "Int32(0)";
    1 = "UInt64(1)";
}
2021-06-25 14:05:31.284719+0800 TS[40537:1225701] oc: set key:__NSCFNumber 1 value:Int32(1)
2021-06-25 14:05:31.284825+0800 TS[40537:1225701] oc:{
    0 = "Int32(0)";
    1 = "Int32(1)";
}
2021-06-25 14:05:31.284888+0800 TS[40537:1225701] oc: set key:__NSCFNumber 0 value:Int64(0)
2021-06-25 14:05:31.284999+0800 TS[40537:1225701] oc:{
    0 = "Int64(0)";
    1 = "Int32(1)";
}
2021-06-25 14:05:31.285050+0800 TS[40537:1225701] oc: set key:__NSCFNumber 1 value:Int64(1)
2021-06-25 14:05:31.285139+0800 TS[40537:1225701] oc:{
    0 = "Int64(0)";
    1 = "Int64(1)";
}
2021-06-25 14:05:31.285204+0800 TS[40537:1225701] oc: set key:__NSCFNumber 0 value:Float32(0)
2021-06-25 14:05:31.285274+0800 TS[40537:1225701] oc:{
    0 = "Float32(0)";
    1 = "Int64(1)";
}
2021-06-25 14:05:31.285323+0800 TS[40537:1225701] oc: set key:__NSCFNumber 1 value:Float32(1)
2021-06-25 14:05:31.285376+0800 TS[40537:1225701] oc:{
    0 = "Float32(0)";
    1 = "Float32(1)";
}
2021-06-25 14:05:31.285429+0800 TS[40537:1225701] oc: set key:__NSCFNumber 0 value:Float64(0)
2021-06-25 14:05:31.285506+0800 TS[40537:1225701] oc:{
    0 = "Float64(0)";
    1 = "Float32(1)";
}
2021-06-25 14:05:31.285583+0800 TS[40537:1225701] oc: set key:__NSCFNumber 1 value:Float64(1)
2021-06-25 14:05:31.285674+0800 TS[40537:1225701] oc:{
    0 = "Float64(0)";
    1 = "Float64(1)";
}
2021-06-25 14:05:31.285795+0800 TS[40537:1225701] oc: result: {
    0 = "Float64(0)";
    1 = "Float64(1)";
}
2021-06-25 14:05:31.285896+0800 TS[40537:1225701] __NSCFNumber 0: Float64(0)
2021-06-25 14:05:31.285999+0800 TS[40537:1225701] __NSCFNumber 1: Float64(1)

結(jié)論

  • @(true) @(false) 類型是 __NSCFNumber
  • [NSNumber numberWithBool:v] 類型是 __NSCFBoolean
  • 有意思的是 相同的操作 swift和oc得到了2種不同的結(jié)果
// swift
__NSCFBoolean 1: true
__NSCFNumber 0: Float64(0)
__NSCFBoolean 0: false
__NSCFNumber 1: Float64(1)

//oc 
__NSCFNumber 0: Float64(0)
__NSCFNumber 1: Float64(1)

  • oc 中 true和1 相等, false和0相等
  • swift 中 true和1不相等格仲, false和0不相等的
  • swift搓蚪、oc uint32、int32、uint64役耕、int64贸铜、float、 double 使用相同的hash孽查、equal規(guī)則

延伸思考

  • 設(shè)計(jì)一種hash算法 實(shí)現(xiàn) uint32饥悴、int32、uint64盲再、int64西设、float、 double 相同值時(shí)hashCode一致
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末答朋,一起剝皮案震驚了整個(gè)濱河市贷揽,隨后出現(xiàn)的幾起案子,更是在濱河造成了極大的恐慌梦碗,老刑警劉巖禽绪,帶你破解...
    沈念sama閱讀 210,835評論 6 490
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件,死亡現(xiàn)場離奇詭異洪规,居然都是意外死亡印屁,警方通過查閱死者的電腦和手機(jī),發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 89,900評論 2 383
  • 文/潘曉璐 我一進(jìn)店門斩例,熙熙樓的掌柜王于貴愁眉苦臉地迎上來雄人,“玉大人,你說我怎么就攤上這事念赶∧埽” “怎么了?”我有些...
    開封第一講書人閱讀 156,481評論 0 345
  • 文/不壞的土叔 我叫張陵晶乔,是天一觀的道長珍坊。 經(jīng)常有香客問我,道長正罢,這世上最難降的妖魔是什么阵漏? 我笑而不...
    開封第一講書人閱讀 56,303評論 1 282
  • 正文 為了忘掉前任,我火速辦了婚禮翻具,結(jié)果婚禮上履怯,老公的妹妹穿的比我還像新娘。我一直安慰自己裆泳,他們只是感情好叹洲,可當(dāng)我...
    茶點(diǎn)故事閱讀 65,375評論 5 384
  • 文/花漫 我一把揭開白布。 她就那樣靜靜地躺著工禾,像睡著了一般运提。 火紅的嫁衣襯著肌膚如雪蝗柔。 梳的紋絲不亂的頭發(fā)上,一...
    開封第一講書人閱讀 49,729評論 1 289
  • 那天民泵,我揣著相機(jī)與錄音癣丧,去河邊找鬼。 笑死栈妆,一個(gè)胖子當(dāng)著我的面吹牛胁编,可吹牛的內(nèi)容都是我干的。 我是一名探鬼主播鳞尔,決...
    沈念sama閱讀 38,877評論 3 404
  • 文/蒼蘭香墨 我猛地睜開眼嬉橙,長吁一口氣:“原來是場噩夢啊……” “哼!你這毒婦竟也來了寥假?” 一聲冷哼從身側(cè)響起市框,我...
    開封第一講書人閱讀 37,633評論 0 266
  • 序言:老撾萬榮一對情侶失蹤,失蹤者是張志新(化名)和其女友劉穎昧旨,沒想到半個(gè)月后,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體祥得,經(jīng)...
    沈念sama閱讀 44,088評論 1 303
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡兔沃,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 36,443評論 2 326
  • 正文 我和宋清朗相戀三年,在試婚紗的時(shí)候發(fā)現(xiàn)自己被綠了级及。 大學(xué)時(shí)的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片乒疏。...
    茶點(diǎn)故事閱讀 38,563評論 1 339
  • 序言:一個(gè)原本活蹦亂跳的男人離奇死亡,死狀恐怖饮焦,靈堂內(nèi)的尸體忽然破棺而出怕吴,到底是詐尸還是另有隱情,我是刑警寧澤县踢,帶...
    沈念sama閱讀 34,251評論 4 328
  • 正文 年R本政府宣布转绷,位于F島的核電站,受9級特大地震影響硼啤,放射性物質(zhì)發(fā)生泄漏议经。R本人自食惡果不足惜,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 39,827評論 3 312
  • 文/蒙蒙 一谴返、第九天 我趴在偏房一處隱蔽的房頂上張望煞肾。 院中可真熱鬧,春花似錦嗓袱、人聲如沸籍救。這莊子的主人今日做“春日...
    開封第一講書人閱讀 30,712評論 0 21
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽蝙昙。三九已至闪萄,卻和暖如春,著一層夾襖步出監(jiān)牢的瞬間耸黑,已是汗流浹背桃煎。 一陣腳步聲響...
    開封第一講書人閱讀 31,943評論 1 264
  • 我被黑心中介騙來泰國打工, 沒想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留大刊,地道東北人为迈。 一個(gè)月前我還...
    沈念sama閱讀 46,240評論 2 360
  • 正文 我出身青樓,卻偏偏與公主長得像缺菌,于是被迫代替她去往敵國和親葫辐。 傳聞我的和親對象是個(gè)殘疾皇子,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 43,435評論 2 348

推薦閱讀更多精彩內(nèi)容