Create a gist now

Instantly share code, notes, and snippets.

Embed
What would you like to do?

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

This comment has been minimized.

Show comment
Hide comment
Owner

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