您的位置:首页 > 移动开发 > Swift

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

2016-10-27 08:11 471 查看
//
//  main.swift
//  SwiftLesson05
//
//  Created by lanou on 16/10/26.
//  Copyright (c) 2016年 lanou. All rights reserved.
//

import Foundation

/*
 函数:
  C语言:封装了一段有特定功能的代码段
  形式:
    返回值类型
函数名(参数列列表){
        代码段
        如果
返回值类型 不是void要return
    }

  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<
b5aa
span class="s1">("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)")
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  ios swift 函数 闭包