码迷,mamicode.com
首页 > 其他好文 > 详细

Swift初体验(二)

时间:2014-06-21 22:53:20      阅读:211      评论:0      收藏:0      [点我收藏+]

标签:函数   闭包   

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

Swift初体验(二),布布扣,bubuko.com

Swift初体验(二)

标签:函数   闭包   

原文地址:http://blog.csdn.net/hmt20130412/article/details/32718761

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!