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

编程入门 行业动态 更新时间:2024-10-23 23:27:52
本文介绍了如何收集函数的返回值(Swift 3)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 我想收集函数的返回值。 问题: 如何在不通过参数的情况下调用'test'函数来收集返回变量'name'?

有没有办法收集变量(值)从函数与agruments(参数),而不通过参数?

我提供了一个例子:

let userName =Jake let userInfo = test(name:userName) func test(name:String) - >字符串{ 返回名称} //函数调用 //目标:我想在不传递参数的情况下检索函数返回值让newUser = test()

是否必须存储函数'test'返回值检索它? 我想检索'userName'Jake

解决方案

您可以 return 像下面这样,

let userName =Jake//类的全局变量 let userInfo = test()// Jake func test() - >字符串{/ /单元素元组将不会有返回标签。 返回self.userName }

如果您喜欢返回与标签然后您需要拖拽或更多返回值如下所示,

func test() - > (name:String,age:Int){ return(self.userName,125)}

通过 test()访问特定的值。name

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

更多推荐

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

本文发布于:2023-11-26 18:30:28,感谢您对本站的认可!
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:函数   返回值   Swift

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!