Swift函数_默认参数
2016-04-21 18:05
405 查看
swift中提供了默认参数功能,在声明函数时给参数指定默认值。
例:
func inputStudentInfo(name:String,age:Int="26")
{
println(name+""+String(age));
}
参数age有默认值。
当下面方式:
inputStudentInfo("bany",27);//error: Missing argument label 'age:' in call
有设置默认值的需加上参数名称:
inputStudentInfo("bany",age:27);//ok!
如果 name和age 都有默认值,就都需要指定参数名称;//but如果声明函数时某参数没有写默认值,指定参数名称会报错哦~
inputStudentInfo(name:"bany",age:27);//error:Extraneous argument label 'age:' in call(参数name没有设置默认值,不需也不可以写参数名称)
例:
func inputStudentInfo(name:String,age:Int="26")
{
println(name+""+String(age));
}
参数age有默认值。
当下面方式:
inputStudentInfo("bany",27);//error: Missing argument label 'age:' in call
有设置默认值的需加上参数名称:
inputStudentInfo("bany",age:27);//ok!
如果 name和age 都有默认值,就都需要指定参数名称;//but如果声明函数时某参数没有写默认值,指定参数名称会报错哦~
inputStudentInfo(name:"bany",age:27);//error:Extraneous argument label 'age:' in call(参数name没有设置默认值,不需也不可以写参数名称)
相关文章推荐
- Swift 定义函数 参数 返回值
- 大数加法、大数乘法、大数减法。Swift。
- Swift 快速学习方法
- 'init(start:end:)' is deprecated: it will be removed in Swift 3. Use the '..<' operator
- swift秒表的实现
- [!] Pods written in Swift can only be integrated as frameworks; add `use_frameworks!` to your Podfil
- Swift中宏#define的替代方案
- swift: @objc的使用
- swift:Swift 2.0值得注意的新特性
- swift 学习计划(一) Array 数组、字典
- IOS(swift)-scrollView(tableView) ·图片加载逻辑的优化
- swift 自定义UIPopoverBackgroundView
- 认识一下Kotlin语言,Android平台的Swift
- 闲话选择排序算法
- swift 数据存储 · NSUserDefaults存储配置信息
- RxSwift 入坑手册 Part0 - 基础概念
- Swift的Guard语句
- Swift方法参数名称省略
- swift 学习计划(一)Array 数组、Dictionary 字典
- Swift 通过出生日期的字符串计算年龄