尝试将可选的Int64分配给字典键时,类型“Int64”和“_”不匹配

时间:2021-12-22 12:10:01

Question regarding Swift 2.1 in Xcode 7.

关于Xcode 7中的Swift 2.1的问题。

I have declared an optional variable like this:

我已经声明了一个像这样的可选变量:

var something: Int64?

I would like to later assign it to a dictionary key using a shorthand if, like this:

我想稍后使用速记将其分配给字典键,如下所示:

dictionary['something'] = (something != nil) ? something! : nil

XCode is giving me the following validation error:

XCode给出了以下验证错误:

Result values in '? :' expression have mismatching types: 'Int64' and '_'

'?中的结果值? :'表达式有不匹配的类型:'Int64'和'_'

What is the issue here? Why can't optional Int64 be nil?

这是什么问题?为什么可选的Int64不能为零?

4 个解决方案

#1


7  

There are a number of problems here. First, Int64 isn't an AnyObject. None of the primitive number types are classes. They can be bridged to AnyObject using NSNumber, but you don't get that bridging automatically for Int64 (see MartinR's comment. I originally said this was because it was wrapped in an Optional, but it's actually because it's fixed-width).

这里有很多问题。首先,Int64不是AnyObject。原始数字类型都不是类。它们可以使用NSNumber桥接到AnyObject,但是你没有为Int64自动桥接(参见MartinR的评论。我原先说这是因为它被包装在一个Optional中,但实际上是因为它是固定宽度的)。

Next, this syntax:

接下来,这个语法:

(something != nil) ? something! : nil

Is just a very complicated way to say something.

说些什么只是一种非常复杂的方式。

The tool you want is map so that you can take your optional and convert it to a NSNumber if it exists.

您想要的工具是map,以便您可以选择并将其转换为NSNumber(如果存在)。

dictionary["something"] = something.map(NSNumber.init)

Of course, if at all possible, get rid of the AnyObject. That type is a huge pain and causes a lot of problems. If this were a [String: Int64] you could just:

当然,如果可能的话,摆脱AnyObject。这种类型是一个巨大的痛苦,并导致很多问题。如果这是[String:Int64],您可以:

dictionary["something"] = something

#2


2  

You can't add a Int64 to a dictionary of type [String : AnyObject], you need to wrap in in an NSNumber object. You can only store objects that conform to AnyObject in your dictionary.

您不能将Int64添加到[String:AnyObject]类型的字典中,您需要将其包含在NSNumber对象中。您只能在字典中存储符合AnyObject的对象。

var something: Int64?

something = 42

if let myVal: Int64 = something { // unwrap to make sure the value is there
    let myNum = NSNumber(longLong: myVal) // create an NSNumber from your Int64

    dictionary["something"] = myNum // insert it into the dictionary
}

As Anton Bronnikov said below, if your dictionary was type [String : Int64], you would be able to add your Int64 to it no problem.

正如Anton Bronnikov所说,如果您的字典是[String:Int64]类型,您可以将Int64添加到它没问题。

#3


1  

It seems that others have already pointed out the issues with the types in your dictionary. I want to add that you can also use the nil coalescing operator '??' as even more concise shorthand for what you are doing in your example. It is most useful when you want to do a check for nil, (and if non-nil) unwrap the value and assign it, otherwise provide a default value.

似乎其他人已经指出了词典中类型的问题。我想补充一点,你也可以使用nil coalescing运算符'??'作为你的例子中你正在做的更简洁的简写。当你想检查nil时,它是最有用的,(如果是非nil)打开值并分配它,否则提供默认值。

var maybeSomething: Int?
var dictionary:[String:Int?] = [:]
dictionary["something"] = maybeSomething ?? nil

#4


0  

something! has type Int64. Not optional Int64, but Int64. nil has type nil. You can't have an expression

什么!类型为Int64。不是可选的Int64,而是Int64。零没有类型。你不能有表达

condition ? Int64 : nil

What would be the type of it?

它的类型是什么?

And the whole thing is pointless. On the right hand side you would have just "something". If that doesn't work then your dictionary doesn't accept optionals.

整件事情毫无意义。在右边你会有“东西”。如果这不起作用,那么你的字典不接受选项。

PS. Noticed that your dictionary wants to store Objective-C objects. In that case, no way it accepts an optional. And it only accepts an Int64 after converting to NSNumber.

PS。注意到您的字典想要存储Objective-C对象。在这种情况下,它无法接受可选项。它只在转换为NSNumber后接受Int64。

#1


7  

There are a number of problems here. First, Int64 isn't an AnyObject. None of the primitive number types are classes. They can be bridged to AnyObject using NSNumber, but you don't get that bridging automatically for Int64 (see MartinR's comment. I originally said this was because it was wrapped in an Optional, but it's actually because it's fixed-width).

这里有很多问题。首先,Int64不是AnyObject。原始数字类型都不是类。它们可以使用NSNumber桥接到AnyObject,但是你没有为Int64自动桥接(参见MartinR的评论。我原先说这是因为它被包装在一个Optional中,但实际上是因为它是固定宽度的)。

Next, this syntax:

接下来,这个语法:

(something != nil) ? something! : nil

Is just a very complicated way to say something.

说些什么只是一种非常复杂的方式。

The tool you want is map so that you can take your optional and convert it to a NSNumber if it exists.

您想要的工具是map,以便您可以选择并将其转换为NSNumber(如果存在)。

dictionary["something"] = something.map(NSNumber.init)

Of course, if at all possible, get rid of the AnyObject. That type is a huge pain and causes a lot of problems. If this were a [String: Int64] you could just:

当然,如果可能的话,摆脱AnyObject。这种类型是一个巨大的痛苦,并导致很多问题。如果这是[String:Int64],您可以:

dictionary["something"] = something

#2


2  

You can't add a Int64 to a dictionary of type [String : AnyObject], you need to wrap in in an NSNumber object. You can only store objects that conform to AnyObject in your dictionary.

您不能将Int64添加到[String:AnyObject]类型的字典中,您需要将其包含在NSNumber对象中。您只能在字典中存储符合AnyObject的对象。

var something: Int64?

something = 42

if let myVal: Int64 = something { // unwrap to make sure the value is there
    let myNum = NSNumber(longLong: myVal) // create an NSNumber from your Int64

    dictionary["something"] = myNum // insert it into the dictionary
}

As Anton Bronnikov said below, if your dictionary was type [String : Int64], you would be able to add your Int64 to it no problem.

正如Anton Bronnikov所说,如果您的字典是[String:Int64]类型,您可以将Int64添加到它没问题。

#3


1  

It seems that others have already pointed out the issues with the types in your dictionary. I want to add that you can also use the nil coalescing operator '??' as even more concise shorthand for what you are doing in your example. It is most useful when you want to do a check for nil, (and if non-nil) unwrap the value and assign it, otherwise provide a default value.

似乎其他人已经指出了词典中类型的问题。我想补充一点,你也可以使用nil coalescing运算符'??'作为你的例子中你正在做的更简洁的简写。当你想检查nil时,它是最有用的,(如果是非nil)打开值并分配它,否则提供默认值。

var maybeSomething: Int?
var dictionary:[String:Int?] = [:]
dictionary["something"] = maybeSomething ?? nil

#4


0  

something! has type Int64. Not optional Int64, but Int64. nil has type nil. You can't have an expression

什么!类型为Int64。不是可选的Int64,而是Int64。零没有类型。你不能有表达

condition ? Int64 : nil

What would be the type of it?

它的类型是什么?

And the whole thing is pointless. On the right hand side you would have just "something". If that doesn't work then your dictionary doesn't accept optionals.

整件事情毫无意义。在右边你会有“东西”。如果这不起作用,那么你的字典不接受选项。

PS. Noticed that your dictionary wants to store Objective-C objects. In that case, no way it accepts an optional. And it only accepts an Int64 after converting to NSNumber.

PS。注意到您的字典想要存储Objective-C对象。在这种情况下,它无法接受可选项。它只在转换为NSNumber后接受Int64。