swift笔记1
个人随记:不喜勿喷 基础数据类型和算法
let yetAnotherPoint = (1,-1) switch yetAnotherPoint { case let (x,y) where x == y:
print("((x),(y)) is on the line x == y") case let (x,y) where x == -y:
print("((x),(y)) is on the line x == -y") case let (x,y):
print("((x),(y)) is just some arbitrary point") }
// 输出 "(1,-1) is on the line x == -y"
11.控制转移语句:continue,break,fall through,return,throw, if #available(iOS 9,OSX 10.10,*) {
// 在 iOS 使用 iOS 9 APIs,并且在 OS X 使用 OS X v10.10 APIs
} else {
// 回滚至早前 iOS and OS X 的API
}
在它普遍的形式中,可用性条件获取了平台名字和版本的清单。平台名字可以是 iOS,OSX 或 watchOS 。除 了特定的主板本号像iOS8,我们可以指定较小的版本号像iOS8.3以及 OS X v10.10.3。 函数函数定义与调用 func sayHello(personName: String) -> String { let greeting = "Hello," + personName + "!" return greeting} 函数的定义,并以 Fun? 作为前缀。指定函数返回类型时,用返回箭头 ? (一个 连字符后跟一个右尖括号)后跟返回类型的名称的方式来表示。 函数参数与返回值 1.多重输入参数:函数多个输入参数,写在->后的圆括号中,用逗号分割 2.带参函数:函数名圆后括号内是参数,没有参数时,括号也不可省略 3.无返回值函数:函数可以没有返回值,没有返回值的函数定义中没有返回箭头-> 例:func printWithoutCounting(stringToPrint: String) { 4.多返回值函数: 例:func minMax(array: [Int]) -> (min: Int,max: Int) 定义一个可以返回数组中最值的函数 注:如果函数返回的元组类型中有可能在整个元组中含有“没有值”,你可以使用可选的(Optional) 元组返回类型反映整个元组可以是 nil 的事实.你可以通过在元组类型的右括号后放置一个问号来定义一个可选元组 例:func minMax(array: [Int]) -> (min: Int,max: Int)? {}这样如果遇到空数组问题,可以返回nil,但是你在调用函数时需要注意nil的处理 函数参数名称 1.指定外部参数名 例:func sayHello(to person: String,and anotherPerson: String) -> String {},这个sayHello(to:and:)使用外部函数名可使得所有参数用一句话表达清楚,使得函数体内部可读,能表达出函数的明确意图. 注:这种写法不常用,有点多余 2.默认参数值:可以在函数体中为每个参数定义 默认值(Deafult Values) 。当默认值被定义后,调用这个函数时可以忽略这个 参数。 例:func someFunction(parameterWithDefault: Int = 12) 3.可变参数:可接受零个或多个值。通过在变量类型名后面加入 (...) 的方式来定义可变参数。函数调用时,你可以用可变参数来传入不确定数 量的输入参数。 例: //计算一组任意长度数字的 算术平均数(arithmetic mean)
func arithmeticMean(numbers: Double...) -> Double { var total: Double = 0
for number in numbers {
total += number }
return total / Double(numbers.count) }
arithmeticMean(1,2,3,4,5)
// returns 3.0,which is the arithmetic mean of these five numbers arithmeticMean(3,8.25,18.75)
// returns 10.0,which is the arithmetic mean of these three numbers
注意: 最多可以有一个可变参数函数,和它必须出现在参数列表中,为了避免歧义在调用函数有多个参数。 如果 你的函数有一个或多个参数有默认值,还有一个可变的参数,将可变参写在参数列表的最后。 4.常量参数和变量参数:** 函数参数默认是常量。试图在函数体中更改参数值将会导致编译错误。这意味着你不能错误地更改参数值。 但是,有时候,如果函数中有传入参数的变量值副本将是很有用的。你可以通过指定一个或多个参数为变量参数,从而避免自己在函数中定义新的变量。变量参数不是常量,你可以在函数中把它当做新的可修改副本来使用。 定义变量参数要在参数前加关键字var,例: func alignRight(var string: String,totalLength: Int,pad: Character) -> String { let amountToPad = totalLength - string.characters.count
if amountToPad < 1 {
return string }
let padString = String(pad) for _ in 1...amountToPad {
string = padString + string }
return string }
let originalString = "hello"
let paddedString = alignRight(originalString,totalLength: 10,pad: "-") // paddedString is equal to "-----hello"
// originalString is still equal to "hello"
5.输入输出函数 定义一个输入输出参数时,在参数定义前加 inout 关键字。一个输入输出参数有传入函数的值,这个值被函数修 改,然后被传出函数,替换原来的值。 你只能将变量作为输入输出参数。你不能传入常量或者字面量(literal value),因为这些量是不能被修改的。当 传入的参数作为输入输出参数时,需要在参数前加 & 符,表示这个值可以被函数修改。例: func swapTwoInts(inout a: Int,inout _ b: Int) { let temporaryA = a
a =b
b = temporaryA
}
函数类型 var mathFunction:(Int,Int)->Int = addTwoInts
print("add Result:(mathFunction(2,3))")
mathFunction = multiplyTowInts
print("multiply Result:(mathFunction(2,3))")
就像其他类型一样,当赋值一个函数给常量或变量时,你可以让 Swift 来推断其函数类型,例: let anotherMathFunction = addTwoInts
// anotherMathFunction is inferred to be of type (Int,Int) -> Int
2.函数类型作为参数类型(Function Types as Parameter Types) 例: func printMathResult(mathFunction: (Int,Int) -> Int,_ a: Int,_ b: Int) { print("Result: (mathFunction(a,b))")} printMathResult(addTwoInts,5) // prints "Result: 8"
3.函数类型作为返回类型 func stepForward(input: Int) -> Int { return input + 1
}
func stepBackward(input: Int) -> Int {
return input - 1 }
func chooseStepFunction(backwards: Bool) -> (Int) -> Int { return backwards ? stepBackward : stepForward
}
//函数调用
var currentValue = 3
let moveNearerToZero = chooseStepFunction(currentValue > 0) // moveNearerToZero now refers to the stepBackward() function
print("Counting to zero:") // Counting to zero: while currentValue != 0 {
print("(currentValue)... ")
currentValue = moveNearerToZero(currentValue) }
print("zero!") // 3...
// 2...
// 1...
// zero!
函数嵌套: 这章中你所见到的所有函数都叫全局函数(global functions),它们定义在全局域中。你也可以把函数定义在别 的函数体中,称作嵌套函数(nested functions)。 默认情况下,嵌套函数是对外界不可见的,但是可以被他们封闭函数(enclosing function)来调用。一个封闭 函数也可以返回它的某一个嵌套函数,使得这个函数可以在其他域中被使用。 你可以用返回嵌套函数的方式重写 chooseStepFunction(_:) 函数:
func chooseStepFunction(backwards: Bool) -> (Int) -> Int {
func stepForward(input: Int) -> Int {
return input + 1
}
func stepBackward(input: Int) -> Int {
}
return backwards ? stepBackward : stepForward
}
var currentValue = -4
let moveNearerToZero = chooseStepFunction(currentValue > 0)
// moveNearerToZero now refers to the nested stepForward() function
while currentValue != 0 {
print("(currentValue)... ")
currentValue = moveNearerToZero(currentValue) }
print("zero!") // -4...
// -3...
// -2...
// -1... // zero!
(编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |