xcode 快速转换为可选值
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/24206353/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Casting to an optional value in swift
提问by Morniak
When I wrote this:
当我写这个:
// json is of type AnyObject
var status : String? = json.valueForKeyPath("status") as String?
Xcode seems to blocked in an infinite loop during the compilation:
Xcode 在编译过程中似乎被无限循环阻塞:
Is there something wrong in my code?
我的代码有问题吗?
I've done some tests and when I wrote:
我做了一些测试,当我写道:
var status : String? = json.valueForKeyPath("status") as? String
Xcode is able to compile but what will be the behavior when valueForKeyPath
will return nil
.
Xcode 能够编译,但是什么时候valueForKeyPath
返回nil
.
回答by Greg
As the documentation says:
正如文档所说:
Because downcasting can fail, the type cast operator comes in two different forms. The optional form, as?, returns an optional value of the type you are trying to downcast to.
Excerpt From: Apple Inc. “The Swift Programming Language.” iBooks. https://itun.es/au/jEUH0.l
因为向下转换可能会失败,所以类型转换运算符有两种不同的形式。可选形式 as? 返回您尝试向下转换的类型的可选值。
摘自:Apple Inc. “The Swift Programming Language”。电子书。https://itun.es/au/jEUH0.l
This means that when you write as? String
, the return value is automatically wrapped in an optional for you, so the actual type produced by this is String?
, which is the behaviour that you want, so your second option is correct.
这意味着当您编写 时as? String
,返回值会自动为您包装在一个可选中,因此由此产生的实际类型是String?
,这是您想要的行为,因此您的第二个选项是正确的。
In your first example, you are trying to cast the value returned by valueForKeyPath
, which is of type AnyObject!
(!
means implicitlyunwrapped), to String?
, which is explicitlywrapped, and this the value is not automatically wrapped in an optional for you, so, since these types are not identical, casting between them probably won't work.
在您的第一个示例中,您试图将 返回的值valueForKeyPath
(类型为AnyObject!
(!
意味着隐式解包))强制转换String?
为显式包装的值,并且该值不会自动包装在您的可选中,因此,由于这些类型不相同,它们之间的转换可能不起作用。
TL;DR: your first option is actually incorrect (although it shouldn't crash the compiler, that's a bug), and your second option is correct and does what you want.
TL;DR:您的第一个选项实际上是不正确的(尽管它不应该使编译器崩溃,这是一个错误),而您的第二个选项是正确的并且可以执行您想要的操作。
回答by drewag
Your second version is correct. An optional value is supposed to be assigned to a statement that can return nil. This is the flow of that line:
你的第二个版本是正确的。应该将可选值分配给可以返回 nil 的语句。这是该行的流程:
- valueForKeyPath returns an implicitly unwrapped optional of AnyObject (
AnyObject!
). This means that it can be nil or it can have a value. - An attempt is made to convert it to a
String
- If it is not possible to convert it to a
String
, returnnil
makingstatus
nil
. If it is indeed a non-nilString
return that makingstatus
whatever string is returned
- valueForKeyPath 返回一个隐式解包的 AnyObject (
AnyObject!
) 选项。这意味着它可以是 nil 或者它可以有一个值。 - 尝试将其转换为
String
- 如果无法将其转换为 a
String
,则返回nil
Makingstatus
nil
。如果它确实是一个非零String
返回,status
则返回任何字符串