Skip to content

Instantly share code, notes, and snippets.

@yanli0303
Last active May 31, 2017 14:58
Show Gist options
  • Save yanli0303/2e844d734a3a993526df85af79143b46 to your computer and use it in GitHub Desktop.
Save yanli0303/2e844d734a3a993526df85af79143b46 to your computer and use it in GitHub Desktop.
Crash Swift compiler by defining a local variable with same name as class property
import Foundation
class Foo {
var bar: Int? = 1
}
enum BarEnum: Int {
case zero = 0
case one = 1
case two = 2
}
extension Foo {
func confuseCompiler() {
// `bar` is a class property, defining a local variable `bar` crashes swift compiler
guard let barEnumRawValue = bar, let bar = BarEnum(rawValue: barEnumRawValue) else {
return
}
print(bar)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment