let http404Error = (statusCode: 404, statusMessage: "Not found")
print(http404Error.0, http404Error.1)
I got some questions and "problems" with tuples:
Question 1:
But what if I want the statusCode to be an Int and an Int only?
Because "statusCode: Int = 404" doesn´t seem to work?
Question 2:
What if I want to shorten the part "print(http404Error.0, http404Error.1)?
Is there a short way to write it, something like print(http404Error.[0, 1])?
Thanks for your help :)
你可以试试
let http404Error:(Int,String) = (statusCode: 404, statusMessage: "Not found")
For specifying the type, you can define a type alias like:
typealias HttpStatus = (statusCode: Int, statusMessage: String)
let http404Error = HttpStatus(403, "Not found")
print(http404Error.0, http404Error.1)
For shortening the print statement, I don't think there is an easy way to do that. One thing you can do is, you can create a custom function which takes the tuple as argument, formats the values and returns a string.
func getStatus(_ status: HttpStatus) -> String {
return "\(status.statusCode) \(status.statusMessage)"
}
print(getStatus(http404Error))
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.