let digitNames = [0: "zero", 1: "one", 2: "two", 3: "three", 4: "Four",
5: "Five", 6: "Six", 7: "Seven", 8: "Eight", 9: "Nine"]
let names = [16, 58, 510]
let newArr = names.map{
(number) -> String in
var number = number
var output = ""
repeat{
output = digitNames[number % 10]! + output
number /= 10
}while number > 0
return output
}
for item in newArr{
print(item)
}
16/10 = 1, 额,这里自动取整了,我还在想怎么处理小数呢
根据数学原理,如果a b均为正整数且a<b时,a % b的值为a
鹅,原来这是乌龟屁股————规定
enum Barcode{
case upc(Int, Int, Int, Int)
case qrCode(String)
}
var productBarcode = Barcode.upc(8, 85909, 51226, 3)
productBarcode = .qrCode("ABCDEFGHIJKLMNOP")
所以,很多看起来很奇怪的东西,百思不得其解时不妨去找找官方,
也许官方已有专门定义,多奇怪都会变得合情合理,当然,也有
强制合理,明明不符合逻辑,官方却说这是龟腚。
闭包,你就感到奇怪,怎么突然间少了(),关键是能运行
func chooseStepFunction(backward: Bool) -> (Int) -> Int {
return backward ? stepBackward : stepForward
}
这一点有助于代码的复用。 这种方法在需要根据不同条件动态选择不同的策略或行为时非常有用。
func stepForward(_ input: Int) -> Int {
return input + 1
}
func stepBackward(_ input: Int) -> Int {
return input - 1
}
var currentValue = 3
let moveNearerToZero = chooseStepFunction(backward: currentValue > 0)
repeat{
currentValue = moveNearerToZero(currentValue)
print("last \(currentValue)")
}while(currentValue > 0)
又悟,所谓的“嵌套函数”指的是里面的那个函数,而外面的那个函数
它叫:外围函数。 nested functions & enclosing functions