Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/116.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/swift/16.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 为什么print()将我的字符串作为可选项打印?_Ios_Swift - Fatal编程技术网

Ios 为什么print()将我的字符串作为可选项打印?

Ios 为什么print()将我的字符串作为可选项打印?,ios,swift,Ios,Swift,我有一个字典,我想使用它的一些值作为另一个字典的键: let key: String = String(dictionary["anotherKey"]) 这里,dictionary[“anotherKey”]是42,但当我在调试器中打印key时,我看到以下内容: (lldb) expression print(key) Optional(42) 这怎么可能?据我所知,String()构造函数确实不返回可选值(并且当我编写let key:String而不是let key:String?时,编

我有一个字典,我想使用它的一些值作为另一个字典的键:

let key: String = String(dictionary["anotherKey"])
这里,
dictionary[“anotherKey”]
42
,但当我在调试器中打印
key
时,我看到以下内容:

(lldb) expression print(key)
Optional(42)
这怎么可能?据我所知,
String()
构造函数确实返回可选值(并且当我编写
let key:String而不是
let key:String?
时,编译器不会抱怨)。有人能解释一下这是怎么回事吗


作为旁注,我目前正在使用
description()
方法解决这个问题。

Swift字典总是返回可选的。

dictionary[“anotherKey”]
提供了
Optional(42)
,因此
String(dictionary[“anotherKey”])
提供的
“Optional(42)”
与预期完全一致(因为可选类型符合StringLiteralConverable,所以您可以获得可选类型的字符串表示)

例如,如果让
打开,则必须使用
打开

if let key = dictionary["anotherKey"] {
    // use `key` here  
}
此时编译器已经知道字典值的类型

如果不是,例如,如果类型是AnyObject,则可以使用
as?字符串

if let key = dictionary["anotherKey"] as? String {
    // use `key` here  
}
或者,如果AnyObject实际上是Int,则作为Int:

if let key = dictionary["anotherKey"] as? Int {
    // use `key` here  
}
或者使用
Int()
将字符串数转换为整数:

if let stringKey = dictionary["anotherKey"], intKey = Int(stringKey) {
    // use `intKey` here  
}

这是出于设计——这是Swift的
字典
的实现方式:

Swift的
字典
类型将其键值下标作为一个接受并返回可选类型的下标来实现。[…]字典
类型使用一个可选的下标类型来模拟并非每个键都有一个值的事实,并提供一种通过为该键指定nil值来删除该键的值的方法。()

如果让构造以去除可选项,则可以在
中展开结果,如下所示:

if let val = dictionary["anotherKey"] {
    ... // Here, val is not optional
}
例如,如果您确信该值存在,因为您在几步之前就将其放入了字典中,那么您可以使用
强制展开操作员以及:

let key: String = String(dictionary["anotherKey"]!)

你误解了结果。
字符串
初始值设定项不返回可选值。它返回可选字符串的字符串表示形式。它是一个非可选字符串,值为“optional(42)”

,如果字典中没有这样的键,也可以使用默认值避免强制展开

var dictionary = ["anotherkey" : 42]

let key: String = 
String(dictionary["anotherkey", default: 0])
print(key)

谢谢我想你的意思是
as?Int
相反,对吗?现在有意义了,我必须打开Int,而不是字符串。但是在我的场景中,我的类型是
AnyObject
,我想将
Int
转换为
字符串(而不是相反)。因此,我使用了您的代码片段,它不作为?字符串
但它无法打开,因此无法工作。谢谢您的回答。你能再核对一下你的最后一行吗?我想你的意思是
let key:String=String(dictionary[“anotherKey”]!)
(注意
)@phi-Ah,当然,感叹号需要在里面。当我尝试用一种对我来说还是比较陌生的语言回答问题时,就会发生这种情况。谢谢记住,人们,添加一条带有否决理由的评论总是有帮助的。我还添加了一些操场代码供参考