But I am still wondering why for that specific method for example, the decision was to crash if a negative number is passed instead of protecting the method call by making it not to accept invalid inputs using
UInt
.
Because once you start adding unsigned in one place it starts to spread through your code like a plague (-: For example, imagine you have an array of strings
a
and you want to create a ‘clear’ copy, where each element in the empty string. You might write this:
let a: [String] = ["Hello", "Cruel", "World!"]
var b = [String](repeating: "", count: a.count)
If the
count
parameter to
init(repeating:count:)
were
UInt
, then the
count
property on the array would have to be
UInt
as well.
Now imagine
b
is used to populate the rows in a
UITableView
. For example:
override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return b.count
}
Now
UITableView
has to use
UInt
to represents rows and row counts. However,
UITableView
makes extensive use of
IndexPath
, meaning that
IndexPath
has to use
UInt
as well. And so on.
Having gone down this path in C I’m very happy with the change of direction that Swift has taken.
Share and Enjoy
—
Quinn “The Eskimo!”
Apple Developer Relations, Developer Technical Support, Core OS/Hardware
let myEmail = "eskimo" + "1" + "@apple.com"