Skip to content

Instantly share code, notes, and snippets.

@kateinoigakukun
Created April 2, 2017 08:10
Show Gist options
  • Save kateinoigakukun/c99904014d538b42e4a02bdd8126f539 to your computer and use it in GitHub Desktop.
Save kateinoigakukun/c99904014d538b42e4a02bdd8126f539 to your computer and use it in GitHub Desktop.
switchはhashValueを見てるのか? #CodePiece
enum Fruit {
case peach
case apple
case grape
}
var i = 3
var emptyEnumPtr = withUnsafePointer(to: &i) { UnsafeRawPointer($0) }
.bindMemory(to: Fruit.self, capacity: MemoryLayout<Fruit>.size)
print(emptyEnumPtr.pointee) // 空
print(emptyEnumPtr.pointee.hashValue) // 0
switch emptyEnumPtr.pointee {
case .peach: print("peach") // peach
case .apple: print("apple")
case .grape: print("grape")
default: print("default")
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment