Swift初体验(二)

//  函数写法初体验
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
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值