swift -函數、函數指標

來源:互聯網
上載者:User

標籤:swift   函數   函數指標   ios開發   

////  main.swift//  FunctionTest-04//import Foundationprintln("函數測試!")testFunction()
////  FunctionTest.swift//  FunctionTest-04//import Foundation//函數以func開頭 表示是一個函數//參數和參數以,//v1 表示參數:String 表示參數的類型// -> 表示傳回值 Stringfunc testConcat(v1:String, v2:String)->String{    return "\(v1) and \(v2)";}//參數是0個,傳回值是三個func getInfo()->(String,String,String){    return ("歡迎","來到","百度");//是一個元祖}//標籤是oc的優點swift把oc裡面的標籤拿過來了func testConcat2(v1 :String, andValue v2 :String,andNumber v3 :Int)->String{    return"\(v1) and \(v2) and \(v3)";}func testFunctionInFunction()->Int{    var y = 10;    //add是一個函數 lua指令碼語言    func add(){        //var y = 1;下面這個y會選擇zui        y += 5;    }    add();    return y;}//這是一個函數 傳回值是(Int->Int)func getFuncPointer()->(Int->Int){    func addOne(num : Int)->Int    {        return num+1;    }    //這裡返回一個函數的地址    return addOne;}func testFunction(){    println("開始測試函數");        let v = testConcat("hello","world");    println(" v is \(v)");        //andValue:稱為一個標籤    let v2 = testConcat2("百度",andValue:"百度招聘",andNumber:100);    println("v2 is \(v2)");        let(v3,v4,v5) = getInfo();    println("v3\(v3) v4\(v4) v5\(v5)");            let v6 = testFunctionInFunction();        println("v6 is\(v6)");        //函數指標    var funcPointer = getFuncPointer();    var v7 = funcPointer(199);    println("v7 is \(v7)");        //把函數地址/指標作為參數    var arr = [20,10,333,4,43,33];    var v8 = hasAnyMatch(arr,lessThanTen);}func hasAnyMatch(list : [Int],condition:Int -> Bool) -> Bool{    for item in list{        if(condition(item))        {            return true;        }    }        return false;    }func lessThanTen(number :Int)->Bool{    return number <= 10;}


著作權聲明:本文為博主原創文章,未經博主允許不得轉載。

swift -函數、函數指標

相關文章

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.