Swift 函数
1: 函数形式:
Swift函数以关键字func 标示。返回类型->后写明。如果没有返回类型可以省去。多个参数用,分割。其中参数名字在前:类型描述
func GetName(strName:String)-> String
{
return "for " + strName
}
let name = GetName("xx")
println("\(name)")
2:函数返回值:函数返回值用->后跟类型说明符号。函数的多返回值。可以用元组返回复合多值。当然也可以没有返回值。
func TestNoneReturn(strName:String)
{
println("\(strName)")
} TestNoneReturn("xx ")
func TestReturn(strName:String)->String
{
return strName + "hello"
} let showName = TestReturn("xx ")
println("\(showName)")
func TestTupleReturn(iAge:Int, strName:String)->(iAgeAdd:Int, strNameFormat:String)
{
let iAgeReturn :Int = iAge +
let strNameReturn :String = " xx oo " + strName; return (iAgeReturn, strNameReturn)
} let tupleReturn = TestTupleReturn(, "yang ")
println("\(tupleReturn.iAgeAdd) \(tupleReturn.strNameFormat)")
3:函数参数
3.1函数参数默认为let类型的。如果你想更改参数副本,那么你要显示使用var修饰。当然也可以不需要参数的函数。如果你像更改参数作为输入输出用inout,调用时候要用取地址符号&
func GetName(var strName:String)-> String
{
strName += " hello"
return "for " + strName
}
let name = GetName("xx")
println("\(name)")
func GetName(inout strName:String)
{
strName += " hello" }
var strTest = "oo"
GetName(&strTest)
println("\(strTest)")
3.2函数参数名字。函数形参名字有本地形参和外部形参之分。
func TestName(strHead:String, strTail:String, strInsert:String)->String{
return strHead + strInsert + strTail
}
let strRes:String = TestName("head ", "tail ", "insert ")
println("\(strRes)")
func TestName(head strHead:String, tail strTail:String,insert strInsert:String)->String{
return strHead + strInsert + strTail
}
let strRes:String = TestName(head:"head ", tail:"tail ", insert:"insert ")
println("\(strRes)")
如果你外部和本地形参名字一样,那么你可以用#简写。
func TestSameName(#name:String, #age:String)->String{
return "name: " + name + "\nage: " + age
}
let strRes = TestSameName(name:"xx", age:"")
println("\(strRes)")
3.3函数参数默认值:如果设置了函数默认值,那么在调用的时候可以省去默认值的调用。
func TestDefaultParam(name:String, defaultName:String = " defaultNM"){
let nameRes = “\n” + name + defaultName;
println(nameRes)
}
TestDefaultParam("mm")
TestDefaultParam("xx", defaultName:" oo")
3.4 可变形参:用"..."代表可变形参。类型一样
func TestMore(numbers:Int...){
var iRes:Int =
for item in numbers {
iRes += item
}
println("\(iRes)")
}
TestMore(,,)
TestMore(,,,,)
4:函数类型:由函数参数类型与顺序以及函数返回值类型
func(iAge:Int, strName:String)->String
{
return "KO"
}
//该函数类型可以记为:(Int, String)->String
在 swift 中您可以像任何其他类型一样的使用函数类型。例如,你可以定义一个常量或变量 为一个函数类型,并为变量指定一个对应函数
func AddTwoInts(a: Int, b: Int) -> Int {
return a + b
}
var AddFunction:(Int, Int)->Int = AddTwoInts
这样你可以用AddFuncton调用方法,其实有点类似与AddFunction是函数AddTwoInts的名字变量
var iRes = AddFunction(1, 4)
同其他变量一样,它还支持类型推断
let AnotherMathFunction = AddTwoInts
var iOther = AnotherMathFunction(5, 6)
函数类型可以作为函数参数使用
func PrintMathResult(MathFunction: (Int, Int) -> Int, a: Int, b: Int)
{
. println("Result: \(MathFunction(a, b))") . }
. PrintMathResult(AddTwoInts, , )
函数类型也可以作为返回类型
func FunctionTest(a:Int, b:Int)->(Int, Int) -> Int{
}
Swift 函数的更多相关文章
- Swift函数编程之Map、Filter、Reduce
在Swift语言中使用Map.Filter.Reduce对Array.Dictionary等集合类型(collection type)进行操作可能对一部分人来说还不是那么的习惯.对于没有接触过函数式编 ...
- 如何在C语言中调用Swift函数
在Apple官方的<Using Swift with Cocoa and Objectgive-C>一书中详细地介绍了如何在Objective-C中使用Swift的类以及如何在Swift中 ...
- swift函数的用法,及其嵌套实例
import Foundation //swift函数的使用 func sayHello(name userName:String ,age:Int)->String{ return " ...
- Swift函数
函数 函数 介绍 // func // 在Swift中,一个个的方法就是函数 // 1.定义函数的关键字是func // 在定义函数的时候,不管有没有参数都加括号,参数写在括号中 // 在定义函数时, ...
- 4 .Swift函数|闭包
在编程中,我们常把能完成某一特定功能的一组代码,并且带有名字标记类型叫做函数,在C语言中,我们知道函数名就是一个指针,它指向了函数体内代码区的第一行代码的地址,在swift中也具有同样的功效. 在Sw ...
- Swift函数|闭包
在编程中,我们常把能完成某一特定功能的一组代码,并且带有名字标记类型叫做函数,在C语言中,我们知道函数名就是一个指针,它指向了函数体内代码区的第一行代码的地址,在swift中也具有同样的功效. 在Sw ...
- Swift函数的定义建议
/* Swift中函数命名的智慧 */ // 1.一般情况下, 我们写一个函数是这么写的 func sayHello(name: String , greeting: String) { print( ...
- Swift 函数和类
函数: func sayHello(personName:String,z:Int)->{ return "hello"+personName+z } print(sayHe ...
- Swift函数柯里化(Currying)简谈
大熊猫猪·侯佩原创或翻译作品.欢迎转载,转载请注明出处. 如果觉得写的不好请多提意见,如果觉得不错请多多支持点赞.谢谢! hopy ;) 下面简单说说Swift语言中的函数柯里化.简单的说就是把接收多 ...
随机推荐
- 将GitLab的数据库导入阿里云PostgreSQL RDS
GitLab的数据库用的是PostgreSQL,之前由于阿里云RDS不支持PostgreSQL,只能将GitLab的数据库部署在云服务器上. 6月1日得知阿里云推出了PostgreSQL RDS,于是 ...
- Configuring Locales
原文地址:http://people.debian.org/~schultmc/locales.html The Easy Way Install debconf (i.e. runapt-get u ...
- 从快的线上callback hell代码说起
概述 就像谈到闭包必须要说js变量作用域一样,谈到 promise 之前肯定要先说谈异步编程.一直以来, javascript 处理异步方式都是使用 callback 方式,对与写 javascrip ...
- python 守护进程 daemon
python 守护进程 daemon # -*-coding:utf-8-*- import sys, os '''将当前进程fork为一个守护进程 注意:如果你的守护进程是由inetd启动的,不要这 ...
- [OpenCV] 1、读取图片
>_<" 安装及配置请看http://www.cnblogs.com/zjutlitao/p/4042074.html >_<" 这篇是一个简单的在VS20 ...
- SignalR初体验
简介 ASP .NET SignalR[1] 是一个ASP .NET 下的类库,可以在ASP .NET 的Web项目中实现实时通信.什么是实时通信的Web呢?就是让客户端(Web页面)和服务器端可以 ...
- paip.python NameError name 'xxx' is not defined\
paip.python NameError name 'xxx' is not defined\ 导入一个另一个文件里面的函数的时候儿,出孪这个err #这个仅仅导入孪file...要使用里面的fun ...
- java继承与多态-3个小题
1.(1)编写一个接口ShapePara,要求: 接口中的方法: int getArea():获得图形的面积.int getCircumference():获得图形的周长 (2)编写一个圆类Circl ...
- Redis & Python/Django 简单用户登陆
一.Redis key相关操作: 1.del key [key..] 删除一个或多个key,如果不存在则忽略 2.keys pattern keys模式匹配,符合glob风格通配符,glob风格的通配 ...
- mac工具收藏
1.office字体兼容 http://mac.pcbeta.com/thread-32703-1-1.html