Swift中Precondition
和throw
之间有什么区别?
考虑这种情况:
func isRange(start: Int, end: Int, value: Bool) -> Bool {
precondition(end > start, "Start greater than end")
if end == start {
return true
}
...
}
V.S。
func isRange(start: Int, end: Int, value: Bool) throws -> Bool {
if (end < start) {
throw Error.IllegalArgumentException(reason: "Start greater than end")
}
if (end == start) {
return true // empty range matches
}
...
}
您更喜欢哪一个?