快速返回一个函数的多个值。

时间:2022-02-01 22:36:50

How do I return 3 separate data values of the same type(Int) from a function in swift?

如何从swift的函数中返回相同类型(Int)的3个单独的数据值?

I'm attempting to return the time of day, I need to return the Hour, Minute and Second as separate integers, but all in one go from the same function, is this possible?

我试图返回一天的时间,我需要返回小时,分钟和秒作为独立的整数,但是所有的一切都来自同一个函数,这可能吗?

I think I just don't understand the syntax for returning multiple values. This is the code I'm using, I'm having trouble with the last(return) line.

我不明白返回多个值的语法。这是我正在使用的代码,我在最后一行(返回)中遇到了麻烦。

Any help would be greatly appreciated!

如有任何帮助,我们将不胜感激!

func getTime() -> Int
{
    let date = NSDate()
    let calendar = NSCalendar.currentCalendar()
    let components = calendar.components(.CalendarUnitHour | .CalendarUnitMinute | .CalendarUnitSecond, fromDate: date)
    let hour = components.hour
    let minute = components.minute
    let second = components.second
    let times:String = ("\(hour):\(minute):\(second)")
    return hour, minute, second
}

4 个解决方案

#1


187  

Return a tuple:

返回一个元组:

func getTime() -> (Int, Int, Int) {
    ...
    return ( hour, minute, second)
}

Then it's invoked as:

然后调用:

let (hour, minute, second) = getTime()

or:

或者:

let time = getTime()
println("hour: \(time.0)")

#2


52  

Also:

另外:

func getTime() -> (hour: Int, minute: Int,second: Int) {
    let hour = 1
    let minute = 2
    let second = 3
    return ( hour, minute, second)
}

Then it's invoked as:

然后调用:

let time = getTime()
print("hour: \(time.hour), minute: \(time.minute), second: \(time.second)")

This is the standard way how to use it in the book The Swift Programming Language written by Apple.

这是如何在苹果编写的《Swift编程语言》一书中使用它的标准方法。

or just like:

或者就像:

let time = getTime()
print("hour: \(time.0), minute: \(time.1), second: \(time.2)")

it's the same but less clearly.

是一样的,但不太清楚。

#3


8  

you should return three different values from this method and get these three in a single variable like this.

您应该从该方法返回三个不同的值,并在这样的单个变量中获得这三个值。

func getTime()-> (hour:Int,min:Int,sec:Int){
//your code
return (hour,min,sec)
}

get the value in single variable

获取单个变量的值

let getTime = getTime()

now you can access the hour,min and seconds simply by "." ie.

现在你可以简单地通过“。”来访问时间、分钟和秒。

print("hour:\(getTime.hour) min:\(getTime.min) sec:\(getTime.sec)")

#4


3  

Swift 3

斯威夫特3

func getTime() -> (hour: Int, minute: Int,second: Int) {
        let hour = 1
        let minute = 20
        let second = 55
        return (hour, minute, second)
    }

To use :

使用方法:

let(hour, min,sec) = self.getTime()
print(hour,min,sec)

#1


187  

Return a tuple:

返回一个元组:

func getTime() -> (Int, Int, Int) {
    ...
    return ( hour, minute, second)
}

Then it's invoked as:

然后调用:

let (hour, minute, second) = getTime()

or:

或者:

let time = getTime()
println("hour: \(time.0)")

#2


52  

Also:

另外:

func getTime() -> (hour: Int, minute: Int,second: Int) {
    let hour = 1
    let minute = 2
    let second = 3
    return ( hour, minute, second)
}

Then it's invoked as:

然后调用:

let time = getTime()
print("hour: \(time.hour), minute: \(time.minute), second: \(time.second)")

This is the standard way how to use it in the book The Swift Programming Language written by Apple.

这是如何在苹果编写的《Swift编程语言》一书中使用它的标准方法。

or just like:

或者就像:

let time = getTime()
print("hour: \(time.0), minute: \(time.1), second: \(time.2)")

it's the same but less clearly.

是一样的,但不太清楚。

#3


8  

you should return three different values from this method and get these three in a single variable like this.

您应该从该方法返回三个不同的值,并在这样的单个变量中获得这三个值。

func getTime()-> (hour:Int,min:Int,sec:Int){
//your code
return (hour,min,sec)
}

get the value in single variable

获取单个变量的值

let getTime = getTime()

now you can access the hour,min and seconds simply by "." ie.

现在你可以简单地通过“。”来访问时间、分钟和秒。

print("hour:\(getTime.hour) min:\(getTime.min) sec:\(getTime.sec)")

#4


3  

Swift 3

斯威夫特3

func getTime() -> (hour: Int, minute: Int,second: Int) {
        let hour = 1
        let minute = 20
        let second = 55
        return (hour, minute, second)
    }

To use :

使用方法:

let(hour, min,sec) = self.getTime()
print(hour,min,sec)