英文:
Cannot convert value of type '(Int, Bool, Int)' to specified type '(Int, String?, String?)'
问题
I have started to explore Swift and I got a task from a lesson:
You are given a tuple and it has 3 parameters:
StatusCode(Int), Message(Optional String), ErrorMessage(Optional String);
In case if the StatusCode is in a range [200...300] then you have to print the Message,
otherwise you have to print the ErrorMessage.
The main idea in the task is that in case if any of last 2 parameters contains not a string then it has to contain nil
because of the Optional String but in my case I get an error:
Cannot convert value of type '(Int, Bool, Int)' to specified type '(Int, String?, String?)'
Here is what I did:
let tupleResponse : (Int, String?, String?) = (200, true, 567)
let (StatusCode, Message, ErrorMessage) = tupleResponse
if tupleResponse.0 >= 200 && tupleResponse.0 <= 300 {
if let checkStr1 = tupleResponse.1 {
tupleResponse.1!
} else {
print("Message is nil")
}
} else {
if let checkStr2 = tupleResponse.2 {
tupleResponse.2!
} else {
print("Error Message is nil")
}
}
英文:
I have started to explore Swift and I got a task from a lesson:
You are given a tuple and it has 3 parameters:
StatusCode(Int), Message(Optional String), ErrorMessage(Optional String);
In case if the StatusCode is in a range [200...300] then you have to print the Message,
otherwise you have to print the ErrorMessage.
The main idea in the task is that in case if any of last 2 parameters contains not a string then <br>
it has to contain nil
because of the Optional String but in my case I get en error:
Cannot convert value of type '(Int, Bool, Int)' to specified type '(Int, String?, String?)'
Here is what I did:
let tupleResponse : (Int, String?, String?) = (200, true, 567)
let (StatusCode, Message, ErrorMessage) = tupleResponse
if tupleResponse.0 >= 200 && tupleResponse.0 <= 300 {
if let checkStr1 = tupleResponse.1 {
tupleResponse.1!
} else {
print("Message is nil")
}
} else {
if let checkStr2 = tupleResponse.2 {
tupleResponse.2!
} else {
print("Error Message is nil")
}
}
I was expecting that in case of "not string" data type it will make it nil
, <br>
however it gives me that error.
答案1
得分: 1
你可以将你的检查合并,使它更简单。
let tupleResponse: (statusCode: Int, message: String?, errorMessage: String?) = (200, "true", "567")
// 检查并解包错误消息,检查无效的状态码
if let errorMessage = tupleResponse.errorMessage, !(200...300).contains(tupleResponse.statusCode) {
print(errorMessage)
}
// 检查有效的状态码,检查并解包消息
else if 200...300 ~= tupleResponse.statusCode, let message = tupleResponse.message {
print(message)
} else { // 由于上述两种情况均检查了所描述的情况,这不应该发生。
print("unexpected responses")
}
在你的代码中,"true, 567" 的类型无效,如果你想发送任何类型。
let tupleResponse: (statusCode: Int, message: Any?, errorMessage: Any?) = (200, true, 567)
if let errorMessage = tupleResponse.errorMessage as? String, !(200...300).contains(tupleResponse.statusCode) {
print(errorMessage)
}
else if 200...300 ~= tupleResponse.statusCode, let message = tupleResponse.message as? String {
print(message)
} else {
print("unexpected responses")
}
但在这里,你将会得到 "unexpected response",因为类型与你期望的不匹配。
let tupleResponse: (statusCode: Int, message: Any?, errorMessage: Any?) = (200, "true", "567")
if let errorMessage = tupleResponse.errorMessage as? String, !(200...300).contains(tupleResponse.statusCode) {
print(errorMessage)
}
else if 200...300 ~= tupleResponse.statusCode, let message = tupleResponse.message as? String {
print(message)
} else {
print("unexpected responses")
}
英文:
You can combine your checks and make it a little simpler.
let tupleResponse : (statusCode: Int, message: String?, errorMessage: String?) = (200, "true", "567")
// Check and unwrap an error message, check invalid status code
if let errorMessage = tupleResponse.errorMessage, !(200...300).contains(tupleResponse.statusCode){
print(errorMessage)
}
// Check for valid status code, check and unwrap the message
else if 200...300 ~= tupleResponse.statusCode, let message = tupleResponse.message {
print(message)
} else { // This should not happen since the above 2 cases check for both described scenarios.
print("unexpected responses")
}
In your code "true, 567" are of invalid types, if you want to send any type.
let tupleResponse : (statusCode: Int, message: Any?, errorMessage: Any?) = (200, true, 567)
if let errorMessage = tupleResponse.errorMessage as? String, !(200...300).contains(tupleResponse.statusCode){
print(errorMessage)
}
else if 200...300 ~= tupleResponse.statusCode, let message = tupleResponse.message as? String{
print(message)
} else {
print("unexpected responses")
}
But here you will get "unexpected response" because the types don't match with what you expect.
let tupleResponse : (statusCode: Int, message: Any?, errorMessage: Any?) = (200, "true", "567")
if let errorMessage = tupleResponse.errorMessage as? String, !(200...300).contains(tupleResponse.statusCode){
print(errorMessage)
}
else if 200...300 ~= tupleResponse.statusCode, let message = tupleResponse.message as? String{
print(message)
} else {
print("unexpected responses")
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论