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没有设置默认值,不需也不可以写参数名称)

相关文章:

  • 2022-12-23
  • 2022-02-14
  • 2021-08-13
  • 2021-12-06
  • 2021-11-29
  • 2021-12-27
  • 2022-01-30
  • 2022-12-23
猜你喜欢
  • 2021-11-29
  • 2021-08-08
  • 2021-07-25
  • 2021-07-15
  • 2021-07-09
  • 2021-10-25
相关资源
相似解决方案