无法将类型为`(Int, Bool, Int)`的值转换为指定类型`(Int, String?, String?)`。

huangapple go评论68阅读模式
英文:

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 &#39;(Int, Bool, Int)&#39; to specified type &#39;(Int, String?, String?)&#39;

Here is what I did:

let tupleResponse : (Int, String?, String?) = (200, true, 567)
let (StatusCode, Message, ErrorMessage) = tupleResponse

if tupleResponse.0 &gt;= 200 &amp;&amp; tupleResponse.0 &lt;= 300 {
    if let checkStr1 = tupleResponse.1 {
        tupleResponse.1!
    } else {
        print(&quot;Message is nil&quot;)
    }
} else {
    if let checkStr2 = tupleResponse.2 {
        tupleResponse.2!
    } else {
        print(&quot;Error Message is nil&quot;)
    }
}

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, &quot;true&quot;, &quot;567&quot;)
// 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(&quot;unexpected responses&quot;)
}

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(&quot;unexpected responses&quot;)
}

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, &quot;true&quot;, &quot;567&quot;)
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(&quot;unexpected responses&quot;)
}

huangapple
  • 本文由 发表于 2023年6月15日 06:48:53
  • 转载请务必保留本文链接:https://go.coder-hub.com/76478038.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定