Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Assertion@Swift

 Assertion@Swift

Assertion/Precondition

Naohiro Hamada

April 28, 2017
Tweet

More Decks by Naohiro Hamada

Other Decks in Programming

Transcript

  1. ASSERTION OR; HOW I LEARNED TO STOP WORRYING AND LOVE

    THE APP CRASH SWIFTѪ޷ձ VOL.18 N. HAMADA
  2. How to Crash an App var instance: String! = nil

    instance.appending("Message")
  3. How to Crash an App var instance: String! = nil

    instance.appending("Message") This is not his story.
  4. How to Crash an App struct Animal { let name:

    String let age: Int } func isAdult(animal: Animal) -> Bool { assert(animal.age >= 0, "Age must be a positive number.") return animal.age >= 20 }
  5. How to Crash an App struct Animal { let name:

    String let age: Int } func isAdult(animal: Animal) -> Bool { assert(animal.age >= 0, "Age must be a positive number.") return animal.age >= 20 } Assertion
  6. Example: assert struct Animal { let name: String let age:

    Int } func isAdult(animal: Animal) -> Bool { assert(animal.age >= 0, "Age must be a positive number.") return animal.age >= 20 }
  7. Example: assert struct Animal { let name: String let age:

    Int } func isAdult(animal: Animal) -> Bool { assert(animal.age >= 0, "Age must be a positive number.") return animal.age >= 20 } animal.age͸೥ྸΛද͍ͯ͠ΔͷͰ ਖ਼ͷ੔਺Ͱͳ͚Ε͹͍͚ͳ͍
  8. assert C෩ͷΞαʔτ conditionِ͕Ͱ͋Ε͹ɺmessageͷ಺༰Λදࣔ͢ Δ condition͕ৗʹِ => assertFailure() func assert(_ condition:

    @autoclosure () -> Bool, _ message: @autoclosure () -> String = default, file: StaticString = #file, line: UInt = #line)
  9. precondition લఏ৚݅ͷνΣοΫΛߦ͏ conditionِ͕Ͱ͋Ε͹ɺmessageͷ಺༰Λදࣔ͢ Δ condition͕ৗʹِ => preconditionFailure() func precondition(_ condition:

    @autoclosure () -> Bool, _ message: @autoclosure () -> String = default, file: StaticString = #file, line: UInt = #line)
  10. Difference: assert and precondition ࠷దԽϨϕϧʹΑͬͯಈ࡞͕ҟͳΔ Optimization assert precondition -Onone condition͕falseͰ͋Ε͹

    ࣮ߦ͕ఀࢭ͞Ε σόοάঢ়ଶʹͳΔ condition͕falseͰ͋Ε͹ ࣮ߦ͕ఀࢭ͞Ε σόοάঢ়ଶʹͳΔ -O condition͸ධՁ͞Εͳ͍ ʢӨڹͳ͠) condition͕falseͰ͋Ε͹ ࣮ߦ͕ఀࢭ -Ounchecked condition͸ৗʹtrueͱ ૝ఆ condition͸ৗʹtrueͱ ૝ఆ
  11. Difference: assert and precondition ࠷దԽϨϕϧʹΑͬͯಈ࡞͕ҟͳΔ Optimization assert precondition -Onone condition͕falseͰ͋Ε͹

    ࣮ߦ͕ఀࢭ͞Ε σόοάঢ়ଶʹͳΔ condition͕falseͰ͋Ε͹ ࣮ߦ͕ఀࢭ͞Ε σόοάঢ়ଶʹͳΔ -O condition͸ධՁ͞Εͳ͍ ʢӨڹͳ͠) condition͕falseͰ͋Ε͹ ࣮ߦ͕ఀࢭ -Ounchecked condition͸ৗʹtrueͱ ૝ఆ condition͸ৗʹtrueͱ ૝ఆ Debugߏ੒ (Default) Releaseߏ੒ (Default)
  12. Example enum UserState { case none case authenticated case superUser

    } func login(userName: String, password: String) { precondition(!userName.isEmpty, "User name must be non-empty string.") precondition(!password.isEmpty, "Password must be non-empty string.") precondition(state == .none, "App state must be non-login state.") // ϩάΠϯॲཧ assert(state != .superUser, "Cannot become super-user immediately.") }
  13. Example enum UserState { case none case authenticated case superUser

    } func login(userName: String, password: String) { precondition(!userName.isEmpty, "User name must be non-empty string.") precondition(!password.isEmpty, "Password must be non-empty string.") precondition(state == .none, "App state must be non-login state.") // ϩάΠϯॲཧ assert(state != .superUser, "Cannot become super-user immediately.") } લఏ৚݅ Ϣʔβʔ໊ɾύεϫʔυ͸ඞਢ ঢ়ଶ͸ະϩάΠϯͰͳ͚Ε͹͍͚ͳ͍
  14. Example enum UserState { case none case authenticated case superUser

    } func login(userName: String, password: String) { precondition(!userName.isEmpty, "User name must be non-empty string.") precondition(!password.isEmpty, "Password must be non-empty string.") precondition(state == .none, "App state must be non-login state.") // ϩάΠϯॲཧ assert(state != .superUser, "Cannot become super-user immediately.") } ࣄޙ৚݅ ϩάΠϯޙɺ͙͢ʹಛݖϢʔβʔʹ͸ͳΕͳ͍