Share
Embed
Transcript
22 slides
-
ASSERTION OR; HOW I LEARNED TO STOP WORRYING AND LOVE
THE APP CRASH SWIFTѪձ VOL.18 N. HAMADA -
ࣗݾհ ໊લ: ·ͩ iOSྺ: 1͘Β͍ʹͳΓ·ͨ͠ Mac࣌ͱ͔͋Γ·ͨ͠ ੲɺHWͱ͔࡞ͬͯͨΓ Qiita http://qiita.com/HaNoHito GitHub
https://github.com/nhamada -
How to Crash an App var instance: String! = nil
instance.appending("Message") -
How to Crash an App var instance: String! = nil
instance.appending("Message") This is not his story. -
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 } -
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 -
Assertion લఏ݅ ਅͱͳΔ͖݅ ද໌ِ͕Ͱ͋Δˠόά Swift assert / assertFailure precondition /
preconditionFailure -
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 } -
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ྸΛද͍ͯ͠ΔͷͰ ਖ਼ͷͰͳ͚Ε͍͚ͳ͍ -
assert C෩ͷΞαʔτ conditionِ͕Ͱ͋Εɺmessageͷ༰Λදࣔ͢ Δ condition͕ৗʹِ => assertFailure() func assert(_ condition:
@autoclosure () -> Bool, _ message: @autoclosure () -> String = default, file: StaticString = #file, line: UInt = #line) -
precondition લఏ݅ͷνΣοΫΛߦ͏ conditionِ͕Ͱ͋Εɺmessageͷ༰Λදࣔ͢ Δ condition͕ৗʹِ => preconditionFailure() func precondition(_ condition:
@autoclosure () -> Bool, _ message: @autoclosure () -> String = default, file: StaticString = #file, line: UInt = #line) -
Difference: assert and precondition ࠷దԽϨϕϧʹΑͬͯಈ࡞͕ҟͳΔ Optimization assert precondition -Onone condition͕falseͰ͋Ε
࣮ߦ͕ఀࢭ͞Ε σόοάঢ়ଶʹͳΔ condition͕falseͰ͋Ε ࣮ߦ͕ఀࢭ͞Ε σόοάঢ়ଶʹͳΔ -O conditionධՁ͞Εͳ͍ ʢӨڹͳ͠) condition͕falseͰ͋Ε ࣮ߦ͕ఀࢭ -Ounchecked conditionৗʹtrueͱ ఆ conditionৗʹtrueͱ ఆ -
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) -
assert vs precondition Ͳ͏͍͏͍͚͕ྑ͍ʁ Usage assert ϩδοΫʹର͢ΔαχςΟνΣοΫ ϝιουͰԿ͔ͷॲཧΛͨ͠ޙͷνΣοΫ precondition ϩδοΫ࣮ߦલͷࣄલ݅ͷνΣοΫ
ϝιουʹ͞ΕͨύϥϝʔλʔͷνΣοΫ ݸਓతʹɺ্هͷΑ͏ͳ͍͚ͯ͠·͢ɻ -
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.") } -
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.") } લఏ݅ Ϣʔβʔ໊ɾύεϫʔυඞਢ ঢ়ଶະϩάΠϯͰͳ͚Ε͍͚ͳ͍ -
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.") } ࣄޙ݅ ϩάΠϯޙɺ͙͢ʹಛݖϢʔβʔʹͳΕͳ͍