Swift初体验(两)

时间:2021-07-25 20:34:44
//  写功能初体验
func getMyName(firstName first:String, lastName last:String) -> String{ //return first + "-" + last
return first + last
}
var myName = getMyName(firstName: "hu", lastName: "mingtao")
println("myName = \(myName)") func getMyName1(#firstName:String, #lastName:String) -> String{ return firstName + lastName
}
var myName1 = getMyName(firstName: "hu", lastName: "mingtao")
println("myName1 = \(myName1)") func getMyName2WithFirstName(firstName:String, #lastName:String) -> String{ //return first + "-" + last
return firstName + lastName
}
var myName2 = getMyName2WithFirstName("hu", lastName: "mingtao")
println("myName2 = \(myName2)") func getMyName3(firstName:String, lastName:String) -> String{ //return first + "-" + last
return firstName + lastName
}
var myName3 = getMyName3("hu", "mingtao")
println("myName3 = \(myName3)") // "默认值"參数在函数调用的时候能够不写,编译器会自己主动调用声明时候设置的默认的值
// 编译器会自己主动为"默认值"參数创建一个和局部參数名称一样的外部參数名称
// 假设不想要编译器自己主动为"默认值"參数创建外部參数名称,那么须要在局部參数名称之前加"_"
func jionString(stringA:String, #stringB:String, _ joiner:String = "-") -> String{ return stringA + joiner + stringB
}
var resultString = jionString("hu", stringB: "mingtao", "100") func caculateMean(numbers: Int...) -> Double{ var sum = 0
for number in numbers{
sum += number
}
return Double(sum) / Double(numbers.count)
}
let mean = caculateMean(1,2,3,4,5,6)
println("\(mean)") func swap(inout a: Int, inout b: Int){ var temp = a
a = b
b = temp
}
var swapedValueA = 3
var swapedValueB = 5
// 交换值,相似于C语言,用&取地址传(代码也有提示功能)
swap(&swapedValueA, &swapedValueB) func add(a:Int, b:Int) -> Int{ return a + b
} func minus(a:Int, b:Int) -> Int{ return a - b
} func multiply(a:Int, b:Int) -> Int{ return a * b
} func divide(a:Int, b:Int) -> Int{ if b != 0{ return a / b
}
return 0
} var operate = add
let addResult = operate(3,5)
println("\(addResult)")
operate = minus
let minusResult = operate(5,3) func getOperate(operation: String) -> ((Int, Int) -> Int)?{ switch(operation){
case "+":
return add
case "-":
return minus
case "*":
return multiply
case "/":
return divide
default:
return nil
} } var operate2 = getOperate("+")
if let realOperate = operate2{
println("result = \(realOperate(3,5))")
} // 运算符重载
let a = 3
let b = 3.5
@infix func + (leftValue: Int, rightValue: Double) -> Double{
return Double(leftValue) + rightValue
}
let sum = a + b // 闭包
func sortArray(array: String[]) -> String[]{ let used: (String, String) -> Bool = {(stringA: String,stringB: String) -> Bool in return stringA > stringB}
let sortedArray = sort(array,{(stringA: String,stringB: String) -> Bool in stringA > stringB})
let sortedArray1 = sort(array,{(stringA: String,stringB: String) -> Bool in return stringA > stringB})
let sortedArray2 = sort(array,{stringA ,stringB in stringA > stringB})
let sortedArray3 = sort(array,{$0 > $1})
let sortedArray4 = sort(array,>)
let sortedArray5 = sort(array){(stringA: String,stringB: String) -> Bool in return stringA > stringB}
return sortedArray
}

版权声明:本文博客原创文章,博客,未经同意,不得转载。