IOS中的Swift基础05(函数,闭包)

//

//  main.swift

//  SwiftLesson05

//

//  Created by lanou on 16/10/26.

//  Copyright (c) 2016 lanou. All rights reserved.

//


import Foundation

/*

 函数:

  C语言:封装了一段有特定功能的代码段

  形式:

    返回值类型 函数名(参数列列表){

        代码段

        如果 返回值类型 不是voidreturn

    }


  swift函数格式:

    func 函数名(参数列表)->返回值类型 {

        功能代码段

        return...

    }

  调用:函数名(参数列表)

*/


//无参无返

//(1)

func printHello()->Void {

    println("hello")

}

printHello()


//(2)

func printHello1(){

    println("hello--1")

}

printHello1()


//(3)

func printHello2()->(){

    println("hello---2")

}

printHello2()


/*

  有参无返

    func 函数名(参数1:数据类型, 参数2:数据类型, ...){

        代码段

    }

*/

//输入月份,打印对应春(1-3)(4-6)(7-9)(10-12),输入月份不合规范的则打印"找智障委员"

func putSeason(month:Int){

    switch month {

        case let temp where temp >= 1 && temp <= 3:

            println("")

        case let temp where temp >= 4 && temp <= 6:

            println("")

        case let temp where temp >= 7 && temp <= 9:

            println("")

        case let temp where temp >= 10 && temp <= 12:

            println("")

        default:

            println("找智障委员")

    }

}

putSeason(6)


/*

  无参有返

    func 函数名()->返回值类型{

        代码段

        return 返回值

   

*/

func peopleCount()->Int{

    return 19

}

//函数有返回值,所以定义一个值来接受

let count = peopleCount()

println(count)


/*

  有参有返回

    func 函数名(参数1:数据类型1, 参数2:数据类型2, ...)->返回值类型{

        代码段

        return 返回值

    }

*/


//示例1:定义一个函数,该函数输入一个月份,返回对应的季节

func monthSeason(numMonth:Int)->String{

    switch numMonth {

        case let temp where temp >= 1 && temp <= 3:

            return ""

        case let temp where temp >= 4 && temp <= 6:

            return ""

        case let temp where temp >= 7 && temp <= 9:

            return ""

        case let temp where temp >= 10 && temp <= 12:

            return ""

        default:

            return "找智障委员"

    }

}

let strSeason = monthSeason(8)

println("该月份为:\(strSeason)")



//示例2:定义一个函数,该函数传入一个字符串,函数在该字符串后面拼接上“Hello”,函数返回新的字符串和心的字符串长度(使用元组)

func pinjie(str1:String)->(String,Int){

    let strResult = str1 + "Hello"

    return (strResult,strResult.lengthOfBytesUsingEncoding(4))

}

let value:(String,Int) = pinjie("World")

println("拼接后的字符串为:\(value.0), 字符串长度为:\(value.1)")


/*

  C语言block的格式:

    返回值类型 (^block变量名)(参数类型1:参数1, 参数类型2:参数2, ...){

        代码段

        return ...

    }


 swift闭包:

    1.block一样也是一种数据类型

    2.跟函数的区别也一样 没有名字


  闭包格式:

    let/var 闭包名 {

        (参数类型1:参数1, 参数类型2:参数2, ...)->返回值类型

        in

        代码段

        return

    }

*/


/*

  无参无反的闭包

    let 闭包名 = {

        ()->()

        in

        代码段

    }

  调用:

    闭包名()

*/

let closure = {

    ()->()

    in

    println("Hello World!")

}

closure()



//有参无返

let closure1 = {

    (month:Int)->()

    in

    switch month {

        case let temp where temp >= 1 && temp <= 3:

            println("Spring")

        case let temp where temp >= 4 && temp <= 6:

            println("summer")

        case let temp where temp >= 7 && temp <= 9:

            println("autumn")

        case let temp where temp >= 10 && temp <= 12:

            println("winter")

        default:

            println("stupid")

    }

}

closure1(8)



//无参有返

let closure2 = {

    ()->String

    in

    return "Hello World!"

}

let str0 = closure2()

println(str0)


//有参有反

let closure3 = {

    (month:Int)->String

    in

    switch month {

    case let temp where temp >= 1 && temp <= 3:

        return "Spring"

    case let temp where temp >= 4 && temp <= 6:

        return "summer"

    case let temp where temp >= 7 && temp <= 9:

        return "autumn"

    case let temp where temp >= 10 && temp <= 12:

        return "winter"

    default:

        return "stupid"

    }

}

let str1 = closure3(6)

println("该季节为:\(str1)")


let closure4 = {

    (str11:String)->(String,Int)

    in

    let strResult = str11 + "Hello"

    return (strResult, strResult.lengthOfBytesUsingEncoding(4))

}

let str2:(String,Int) = closure4("World")

println("拼接后:\(str2.0), 长度:\(str2.1)")





  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值