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

Assertion@Swift

 Assertion@Swift

Assertion/Precondition

1516d3136f143834c8af104b68468aa6?s=128

Naohiro Hamada

April 28, 2017
Tweet

Transcript

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

    THE APP CRASH SWIFTѪ޷ձ VOL.18 N. HAMADA
  2. ࣗݾ঺հ ໊લ: ͸·ͩ iOSྺ: 1೥൒͘Β͍ʹͳΓ·ͨ͠ Mac࣌୅ͱ͔΋͋Γ·ͨ͠ ੲ͸ɺHWͱ͔࡞ͬͯͨΓ Qiita http://qiita.com/HaNoHito GitHub

    https://github.com/nhamada
  3. Agenda ʢ։ൃத͸ʣؾܰʹΫϥογϡͤ͞Α͏

  4. How to Crash an App var instance: String! = nil

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

    instance.appending("Message") This is not his story.
  6. 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 }
  7. 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
  8. Assertion લఏ৚݅ ਅͱͳΔ΂͖৚݅ ද໌ِ͕Ͱ͋Δˠόά Swift assert / assertFailure precondition /

    preconditionFailure
  9. 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 }
  10. 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͸೥ྸΛද͍ͯ͠ΔͷͰ ਖ਼ͷ੔਺Ͱͳ͚Ε͹͍͚ͳ͍
  11. assert and precondition

  12. assert C෩ͷΞαʔτ conditionِ͕Ͱ͋Ε͹ɺmessageͷ಺༰Λදࣔ͢ Δ condition͕ৗʹِ => assertFailure() func assert(_ condition:

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

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

    ࣮ߦ͕ఀࢭ͞Ε σόοάঢ়ଶʹͳΔ condition͕falseͰ͋Ε͹ ࣮ߦ͕ఀࢭ͞Ε σόοάঢ়ଶʹͳΔ -O condition͸ධՁ͞Εͳ͍ ʢӨڹͳ͠) condition͕falseͰ͋Ε͹ ࣮ߦ͕ఀࢭ -Ounchecked condition͸ৗʹtrueͱ ૝ఆ condition͸ৗʹtrueͱ ૝ఆ
  15. 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)
  16. assert vs precondition Ͳ͏͍͏࢖͍෼͚͕ྑ͍ʁ Usage assert ϩδοΫʹର͢ΔαχςΟνΣοΫ ϝιου಺ͰԿ͔ͷॲཧΛͨ͠ޙͷνΣοΫ precondition ϩδοΫ࣮ߦલͷࣄલ৚݅ͷνΣοΫ

    ϝιουʹ౉͞ΕͨύϥϝʔλʔͷνΣοΫ ݸਓతʹɺ্هͷΑ͏ͳ࢖͍෼͚ͯ͠·͢ɻ
  17. Example ϩάΠϯը໘ ཁ݅ Ϣʔβʔͷঢ়ଶ ະϩάΠϯ ϩάΠϯࡁΈ ಛݖϢʔβʔ ϩάΠϯͰ͖Δͷ͸ɺະϩάΠϯͷͱ͖ ϩάΠϯը໘͔Β͸ಛݖϢʔβʔʹͳΕͳ ͍

  18. 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.") }
  19. 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.") } લఏ৚݅ Ϣʔβʔ໊ɾύεϫʔυ͸ඞਢ ঢ়ଶ͸ະϩάΠϯͰͳ͚Ε͹͍͚ͳ͍
  20. 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.") } ࣄޙ৚݅ ϩάΠϯޙɺ͙͢ʹಛݖϢʔβʔʹ͸ͳΕͳ͍
  21. Pros and Cons Pros ໰୊ՕॴΛૣظʹɺଈ࠲ʹಛఆͰ͖Δ σόοά͕ϥΫʹͳΔ ίʔυ্ʹઃܭ࣌ͷ൑அΛ࢒ͤΔ Cons ίʔυॻ͘ྔ͕૿͑Δ ঢ়گʹΑͬͯ͸ɺͻΒ͢ΒΫϥογϡͯ͠ʢQAςετதʣπϥΠ

    ޻ظ͕ݫ͍͠৔߹ɺͳͲɺπϥϛ͔͠ͳ͍
  22. Summary assert/preconditionΛ͏·͘࢖͍ͬͯ͜͏ σόοάͰָ͕Ͱ͖ΔΑ͏ʹ ઃܭ্ͷ൑அΛίʔυʹ࢒͢