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)")
// 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(二)-函数
- iOS学习笔记42-Swift(二)函数和闭包
- IOS-函数和闭包(基础一)
- iOS学习笔记42-Swift(二)函数和闭包
- 学校学习ios教程笔记,第六节swift中的函数以及闭包
- Swift 基础语法三--函数和闭包
- iOS巅峰之Swift 函数, 闭包详解(下)
- swift基础语法(四) 函数、闭包(Closures)
- Swift语法基础入门三(函数, 闭包)
- Swift基础语法: 23 - Swift的闭包简介, Sorted函数, 闭包的表达式语法, 根据上下文推断类型, 参数名简写, 运算符函数
- iOS学习笔记42-Swift(二)函数和闭包
- swift 学习(二)基础知识 (函数,闭包,ARC,柯里化,反射)
- swift基础学习(04)[闭包、函数、枚举、类和结构体]
- Swift语法基础:3 - Swift的函数和闭包
- Swift基础(八)函数和闭包
- Swift开发IOS基础(动态更新)
- 黑马程序员——零基础学习iOS开发——05 C语言:进制、内存储存细节、类型说明符、位运算、数组
- iOS swift零基础学习心得
- 函数05 - 零基础入门学习C语言36
- Swift基础语法: 22 - Swift的函数类型, 嵌套函数