Skip to content

Instantly share code, notes, and snippets.

@SimplGy
Created September 4, 2016 21:15
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save SimplGy/934af7b6bd43b1296a4fa168fd2137bc to your computer and use it in GitHub Desktop.
Save SimplGy/934af7b6bd43b1296a4fa168fd2137bc to your computer and use it in GitHub Desktop.

No Runtime Error:

func == (l: Car, r: Car) -> Bool { return l.vin == r.vin }
struct Car { let vin: String }
protocol hasDriversLicense: class {
  var car: Car? { get set }
}
class YoungPro: hasDriversLicense {
  var car: Car? { didSet {
    if let oldValue = oldValue, car = car, oldValue == car { return }
    print("car changed")
  }}
  init(){}
}
let you = YoungPro()
you.car = Car(vin: "abc123")

Runtime error:

func == (l: Car?, r: Car?) -> Bool {
  if l == nil && r == nil { return true }
  guard let l = l, let r = r else { return false }
  return l.vin == r.vin
}
struct Car { let vin: String } // class, struct, tuple... doesn't matter

protocol hasDriversLicense: class {
  var car: Car? { get set }
}

class YoungPro: hasDriversLicense {
  var car: Car? { didSet {
    if oldValue == car { return }
    print("car changed")
  }}
  init(){}
}

let you = YoungPro()
you.car = Car(vin: "abc123") // EXC_BAD_ACCESS code: 2
@SimplGy
Copy link
Author

SimplGy commented Sep 4, 2016

Formatted code for bug report: https://bugs.swift.org/browse/SR-2559

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment