Function 从swift中的函数返回多个值
如何从swift中的函数返回3个相同类型(Int)的单独数据值 我试图返回一天中的时间,我需要返回小时、分钟和秒作为单独的整数,但是从同一个函数一次完成,这可能吗 我想我只是不理解返回多个值的语法。这是我正在使用的代码,最后一行(返回)有问题 任何帮助都将不胜感激Function 从swift中的函数返回多个值,function,swift,return,Function,Swift,Return,如何从swift中的函数返回3个相同类型(Int)的单独数据值 我试图返回一天中的时间,我需要返回小时、分钟和秒作为单独的整数,但是从同一个函数一次完成,这可能吗 我想我只是不理解返回多个值的语法。这是我正在使用的代码,最后一行(返回)有问题 任何帮助都将不胜感激 func getTime() -> Int { let date = NSDate() let calendar = NSCalendar.currentCalendar() let components
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
}
返回元组:
func getTime() -> (Int, Int, Int) {
...
return ( hour, minute, second)
}
然后它被调用为:
let (hour, minute, second) = getTime()
let time = getTime()
print("hour: \(time.hour), minute: \(time.minute), second: \(time.second)")
或:
此外:
然后它被调用为:
let (hour, minute, second) = getTime()
let time = getTime()
print("hour: \(time.hour), minute: \(time.minute), second: \(time.second)")
这是苹果公司编写的《Swift编程语言》一书中使用它的标准方法 或者就像:
let time = getTime()
print("hour: \(time.0), minute: \(time.1), second: \(time.2)")
这是相同的,但不太清楚。您应该从这个方法返回三个不同的值,并在一个变量中得到这三个值,就像这样
func getTime()-> (hour:Int,min:Int,sec:Int){
//your code
return (hour,min,sec)
}
获取单个变量中的值
let getTime = getTime()
现在,您只需通过“.ie”即可访问小时、分钟和秒
Swift 3
func getTime() -> (hour: Int, minute: Int,second: Int) {
let hour = 1
let minute = 20
let second = 55
return (hour, minute, second)
}
使用:
let(hour, min,sec) = self.getTime()
print(hour,min,sec)
更新Swift 4.1 在这里,我们创建一个结构来实现元组用法并验证OTP文本长度。对于本例,它需要有两个字段
struct ValidateOTP {
var code: String
var isValid: Bool }
func validateTheOTP() -> ValidateOTP {
let otpCode = String(format: "%@%@", txtOtpField1.text!, txtOtpField2.text!)
if otpCode.length < 2 {
return ValidateOTP(code: otpCode, isValid: false)
} else {
return ValidateOTP(code: otpCode, isValid: true)
}
}
希望有帮助
感谢return NSArray(对象:小时、分钟、秒),“Swift编程语言”iBook实际上有一个标题为“具有多个返回值的函数”的部分……请注意,更好的解决方案可能是定义一个时间结构并使用它而不是元组。您如何从objective C方法调用该Swift func?想知道如何获得三个返回值的句柄。例如,你会用字典吗?你不会。如果要从objc调用它,必须返回一个不确定的类(可能是结构)。建议在参数中添加名称:func getTime()->(hour:Int,minute:Int,second:Int),然后像getTime()一样。hour@BrunoLemos嗯,我仍然会选择更好的解决方案,即定义一个时间结构并使用它。
正确的答案无法在swift 2.0中正确编译:错误,在swift 2中返回一个未命名的元组与在swift 1中的工作原理相同。看这个。是的,你是对的,但是我在相似性上下文中得到了一个编译错误。我应该找到原因。编译错误与此无关,这是因为CalendarUnit格式在Swift 2中发生了更改,而不是元组格式。不像那样,我所说的编译错误出现在代码中。time.hour
不再起作用。请更新代码。输出:印度和加拿大的天气差异为27.0,以便更好地理解:
//By : Dhaval Nimavat
import UIKit
func weather_diff(country1:String,temp1:Double,country2:String,temp2:Double)->(c1:String,c2:String,diff:Double)
{
let c1 = country1
let c2 = country2
let diff = temp1 - temp2
return(c1,c2,diff)
}
let result =
weather_diff(country1: "India", temp1: 45.5, country2: "Canada", temp2: 18.5)
print("Weather difference between \(result.c1) and \(result.c2) is \(result.diff)")
struct ValidateOTP {
var code: String
var isValid: Bool }
func validateTheOTP() -> ValidateOTP {
let otpCode = String(format: "%@%@", txtOtpField1.text!, txtOtpField2.text!)
if otpCode.length < 2 {
return ValidateOTP(code: otpCode, isValid: false)
} else {
return ValidateOTP(code: otpCode, isValid: true)
}
}
let isValidOTP = validateTheOTP()
if isValidOTP.isValid { print(" valid OTP") } else { self.alert(msg: "Please fill the valid OTP", buttons: ["Ok"], handler: nil)
}