简体   繁体   English

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

[英]Mismatching types 'Int64' and '_' when trying to assign optional Int64 to a dictionary key

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不能为零?

There are a number of problems here. 这里有很多问题。 First, Int64 isn't an AnyObject . 首先, Int64不是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). 它们可以使用NSNumber桥接到AnyObject ,但是你没有为Int64自动桥接(参见MartinR的评论。我原先说这是因为它被包装在一个Optional中,但实际上是因为它是固定宽度的)。

Next, this syntax: 接下来,这个语法:

(something != nil) ? something! : nil

Is just a very complicated way to say something . 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 . 当然,如果可能话,摆脱了AnyObject That type is a huge pain and causes a lot of problems. 这种类型是一个巨大的痛苦,并导致很多问题。 If this were a [String: Int64] you could just: 如果这是[String: Int64]你可以只:

dictionary["something"] = something

You can't add a Int64 to a dictionary of type [String : AnyObject] , you need to wrap in in an NSNumber object. 您不能将Int64添加到[String : AnyObject]类型的字典中,您需要将其包含在NSNumber对象中。 You can only store objects that conform to AnyObject in your dictionary. 您只能在字典中存储符合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. 正如安东·布朗尼科夫在下面所说,如果你的字典是[String : Int64]类型,你可以将Int64添加到它没有问题。

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 '??' 我想补充一点,你也可以使用nil coalescing运算符'??' 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时,它是最有用的,(如果是非nil)打开值并分配它,否则提供默认值。

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

something! 什么! has type Int64. 有类型Int64。 Not optional Int64, but Int64. 不是可选的Int64,而是Int64。 nil has type nil. 零没有类型。 You can't have an expression 你不能有表达

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. PS。 Noticed that your dictionary wants to store Objective-C objects. 注意到您的字典想要存储Objective-C对象。 In that case, no way it accepts an optional. 在这种情况下,它无法接受可选项。 And it only accepts an Int64 after converting to NSNumber. 它只在转换为NSNumber后接受Int64。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM