Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/117.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios 二进制运算符不能应用于Int和String类型的操作数-Swift 2.3->;Swift 3.2转换错误_Ios_Swift_Swift3.2 - Fatal编程技术网

Ios 二进制运算符不能应用于Int和String类型的操作数-Swift 2.3->;Swift 3.2转换错误

Ios 二进制运算符不能应用于Int和String类型的操作数-Swift 2.3->;Swift 3.2转换错误,ios,swift,swift3.2,Ios,Swift,Swift3.2,从Swift 2.3转换到3.2时,我收到以下错误 错误:二进制运算符不能应用于Int和String类型的操作数 对于此if条件,即if(错误?.code)!=“-112”如下行所示 if (error?.code)! == "-112" { print("hello") } 错误本身表示它是不同类型的Int和String 您可能需要以相同的形式键入一个或另一个,然后进行比较 if (String(error?.code)!) == "-112"){ print("hello"

从Swift 2.3转换到3.2时,我收到以下错误

错误:二进制运算符不能应用于Int和String类型的操作数

对于此if条件,即
if(错误?.code)!=“-112”
如下行所示

if (error?.code)! == "-112" 
{
     print("hello")
}

错误本身表示它是不同类型的
Int
String

您可能需要以相同的形式键入一个或另一个,然后进行比较

if (String(error?.code)!) == "-112"){
  print("hello")
} 

错误本身表示它是不同类型的
Int
String

您可能需要以相同的形式键入一个或另一个,然后进行比较

if (String(error?.code)!) == "-112"){
  print("hello")
} 

您需要键入将错误代码结果转换为字符串,如下所示:

if String(error?.code)!) == "-112" {
print("Hello")
}
本质上,您是将错误?代码“转换”为字符串,方法是将其放置在字符串“容器模具”中并展开值(检索转换结果)


此外,如果使用的是API响应,则必须在else/if语句中说明所有其他错误代码,以确保正确处理所有响应(以防万一)。

您需要键入将错误代码结果转换为字符串,如下所示:

if String(error?.code)!) == "-112" {
print("Hello")
}
本质上,您是将错误?代码“转换”为字符串,方法是将其放置在字符串“容器模具”中并展开值(检索转换结果)


此外,如果使用的是API响应,则必须说明else/if语句中的所有其他错误代码,以确保正确处理所有响应(以防万一)。

Swift是一种具有强类型系统的语言。只能比较相同类型的值

由于左侧是
Int
,因此请在右侧使用
Int
值。创建一个字符串是不必要的昂贵。不要那样做

最有效(和安全)的解决方案是

if error?.code == -112 
{
     print("hello")
}

Swift是一种具有强大类型系统的语言。只能比较相同类型的值

由于左侧是
Int
,因此请在右侧使用
Int
值。创建一个字符串是不必要的昂贵。不要那样做

最有效(和安全)的解决方案是

if error?.code == -112 
{
     print("hello")
}

转换您的
(错误?.code)到字符串-->
字符串((错误?.code)!)
并检查
是否(错误?.code)!=-112
也应该这样做:)谢谢@Anbu.karthik。是的,谢谢你转换你的
(错误?.code)到字符串-->
字符串((错误?.code)!)
并检查
是否(错误?.code)!=-112
也应该这样做:)谢谢@Anbu.karthik。是的,很有效谢谢