Last active
April 30, 2018 07:05
-
-
Save ktanaka117/782a095b922592679d90d9acf4b17977 to your computer and use it in GitHub Desktop.
==と===に関する同値と同一の確認。UIViewはクラスなはずなのに、==が使えるのはなぜかと、===が何を同一としているのかを調べてみた。
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 参考・関連 | |
// https://qiita.com/omochimetaru/items/2aa00c7cb0eef4e57999 | |
// https://qiita.com/omochimetaru/items/c95e0d36ae7f1b1a9052 | |
// https://stackoverflow.com/questions/24938503/nsobject-is-hashable-but-a-protocol-that-adopts-nsobject-is-not | |
// https://developer.apple.com/documentation/swift/1538988 | |
import Foundation | |
class Hoge {} | |
let hoge1 = Hoge() | |
let hoge2 = Hoge() | |
let hoge3 = hoge1 | |
let hoge1Ref: Unmanaged<Hoge> = Unmanaged<Hoge>.passUnretained(hoge1) | |
let rawHoge1: UnsafeMutableRawPointer = hoge1Ref.toOpaque() | |
let hoge3Ref: Unmanaged<Hoge> = Unmanaged<Hoge>.passUnretained(hoge3) | |
let rawHoge3: UnsafeMutableRawPointer = hoge3Ref.toOpaque() | |
let hoge2Ref: Unmanaged<Hoge> = Unmanaged<Hoge>.passUnretained(hoge2) | |
let rawHoge2: UnsafeMutableRawPointer = hoge2Ref.toOpaque() | |
//hoge1 == hoge2 | |
//hoge1 == hoge3 | |
hoge1 === hoge2 | |
hoge1 === hoge3 | |
// hoge1.hash | |
// hoge2.hash | |
// hoge3.hash | |
// hoge1.hashValue | |
// hoge2.hashValue | |
// hoge3.hashValue | |
import UIKit | |
// UIViewはNSObjectを継承していて、NSObjectがEquatableにも準拠しているので==が使える | |
let v1 = UIView() | |
let v2 = UIView() | |
let v3 = v1 | |
let v1Ref: Unmanaged<UIView> = Unmanaged<UIView>.passUnretained(v1) | |
let rawV1: UnsafeMutableRawPointer = v1Ref.toOpaque() | |
let v3Ref: Unmanaged<UIView> = Unmanaged<UIView>.passUnretained(v3) | |
let rawV3: UnsafeMutableRawPointer = v3Ref.toOpaque() | |
let v2Ref: Unmanaged<UIView> = Unmanaged<UIView>.passUnretained(v2) | |
let rawV2: UnsafeMutableRawPointer = v2Ref.toOpaque() | |
v1 == v2 | |
v1 == v3 | |
v1 === v2 | |
v1 === v3 | |
v1.hash | |
v2.hash | |
v3.hash | |
v1.hashValue | |
v2.hashValue | |
v3.hashValue |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment