swift 返回值是函数_如何收集函数的返回值(Swift 3)

Goal:

I want to collect the return value of a function.

Question:

How can I call the 'test' function to collect the return variable 'name' without passing through a parameter?

Is there a way to collect a variable(values) from functions with agruments(parameters) without passing through a parameter?

I have provided an example:

let userName = "Jake"

let userInfo = test(name: userName)

func test(name: String) -> String {

return name

}

// function call

// Goal: I want to retrieve the function return value without passing a parameter

let newUser = test()

Does the function 'test' return value have to be stored to retrieve it?

I want to retrieve the 'userName' Jake

解决方案

You can return like below,

let userName = "Jake" //Global variable of the class

let userInfo = test() //Jake

func test() -> String { //single - element tuple will don't have label for return.

return self.userName

}

If you like to return with labels then you need tow or more return values like below,

func test() -> (name:String,age:Int) {

return (self.userName,125)

}

And access specific values by test().name

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值