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语言中的函数柯里化.简单的说就是把接收多 ...
随机推荐
- 单元测试中使用Moq对EF的DbSet进行mock
刚用上Moq,就用它解决了一个IUnitOfWork的mock问题,在这篇博文中记录一下. 开发场景 Application服务层BlogCategoryService的实现代码如下: public ...
- Kali Linux Web 渗透测试视频教程— 第十三课-密码破解
Kali Linux Web 渗透测试— 第十三课-密码破解 文/玄魂 目录 Kali Linux Web 渗透测试— 第十三课-密码破解............................... ...
- shellKali Linux Web 渗透测试— 初级教程(第三课)
shellKali Linux Web 渗透测试— 初级教程(第三课) 文/玄魂 目录 shellKali Linux Web 渗透测试—初级教程(第三课) 课程目录 通过google hack寻找测 ...
- Eclipse在线安装SVN
一.SVN在线安装 下面为在线安装SVN插件.以下是在线安装步骤: 其中http://subclipse.tigris.org/update_1.10.x是最新版本的SVN插件的下载站点[subcli ...
- [游戏模版18] Win32 五子棋
>_<:Learning its AI logic. >_<:resource >_<:code: #include <windows.h> // C ...
- tomcat通过conf-Catalina-localhost目录发布项目详解
Tomcat发布项目的方式大致有三种,但小菜认为通过在tomcat的conf/Catalina/localhost目录下添加配置文件,来发布项目,是最佳选择. 因为这样对tomcat的入侵性最小,只需 ...
- js 函数提升和变量提升
总结: 函数提升比变量提升优先级高! 词法分析 词法分析方法: js运行前有一个类似编译的过程即词法分析,词法分析主要有三个步骤: 分析参数 再分析变量的声明 分析函数说明 具体步骤如下: 函数在运行 ...
- phpcms v9二次开发笔记
phpcms是基于MVC结构的. 安装: 下载phpcms_v9.5.9_UTF8.zip:新建目录phpcms,将压缩包里install_package目录下所有文件复制到phpcms目录.浏览器输 ...
- 深入理解HTML5:语义、标准与样式(勇猛精进早登大师殿堂创最优品质交互)
深入理解HTML5:语义.标准与样式(勇猛精进早登大师殿堂创最优品质交互) [美]布拉德福(Bradford,A.) [美]海涅(Haine,P.)著 高京译 ISBN 978-7-121-20552 ...
- D. Book of Evil
D. Book of Evil time limit per test 2 seconds memory limit per test 256 megabytes input standard inp ...