斯威夫特2:!,? -"可选类型"..."的值尚未展开" [英] Swift 2: !, ? -" Value of optional type "..." not unwrapped"

查看:80
本文介绍了斯威夫特2:!,? -"可选类型"..."的值尚未展开"的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

未包装的可选类型"..."的值;你的意思是使用'!'还是?"?

Value of optional type "..." not unwrapped; did you mean to use '!' or '?'?

有人可以向我解释此错误消息的确切含义吗?我什么时候应该使用?"什么时候是!"?

Can someone explain me what this error message exactly means? When should I use '?' and when '!'?

推荐答案

obj?.fn()如果对象不为null,则调用fn成员函数,否则不做任何事情.

obj?.fn() calls fn member function if the object isn't null, otherwise doesn't do anything.

obj!.fn()断言obj不为null,并调用fn.如果对象为空,则会出现异常.

obj!.fn() on the other hand asserts that obj isn't null, and calls fn. If the object is null, you get an exception.

因此,在自信性上是有区别的:您可以询问或简单地声明可为空的对象的可为空的属性.

So it's a difference in assertiveness: you either ask or simply claim the nullable property of a nullable object.

这篇关于斯威夫特2:!,? -"可选类型"..."的值尚未展开"的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆