Syntax 是';x?.y';使用与'相同的Swift;x?';然后是';。y';?
我理解Swift,如果我定义Syntax 是';x?.y';使用与'相同的Swift;x?';然后是';。y';?,syntax,swift,member,optional,Syntax,Swift,Member,Optional,我理解Swift,如果我定义 var opt:String? = "Optional" 如果我尝试,我将得到一个错误 opt.isEmpty 因为opt属于String?类型,它没有isEmpty方法。我想我明白这一点 opt?.isEmpty 不会产生错误,因为opt?展开(任何非nil)opt,导致字符串,该字符串具有isEmpty方法。但是 opt? 它本身会产生一个字符串?,而不是字符串 ?。与紧跟在之后的是不同的运算符吗 试一试 并获取一个提示性错误。是opt?.isEmp
var opt:String? = "Optional"
如果我尝试,我将得到一个错误
opt.isEmpty
因为opt
属于String?
类型,它没有isEmpty
方法。我想我明白这一点
opt?.isEmpty
不会产生错误,因为opt?
展开(任何非nil
)opt
,导致字符串
,该字符串具有isEmpty
方法。但是
opt?
它本身会产生一个字符串?
,而不是字符串
?。
与紧跟在之后的
是不同的运算符吗
试一试
并获取一个提示性错误。是opt?.isEmpty
不同于字符串?
。它被称为。来自swift编程指南:
您可以通过在后面放置问号(?)来指定可选链接
要对其调用属性、方法或属性的可选值
如果可选项为非nil,则为下标。这与放置非常相似
可选值后的感叹号(!)可强制
展开其值。主要区别在于可选的链接
当可选值为nil时正常失败,而强制展开
当可选值为nil时触发运行时错误
这会根据您的想法创建可选选项
var-opt:String
现在
与除了相同
opt!.isEmpty //it will crash if opt is nil
如果opt代码>为nil
时,它不会在运行时崩溃。可选链用于在不调用的情况下调用可选的长序列。每个可选链返回可选的,即opt?返回可选的展开,如果为nil,则调用isEmpty,否则调用isEmpty和reutns值。
也
当您在写上面的语句时,它只是可选的(可选的不是可选的),由于大括号的原因,它无法展开。所以显示错误
$T2?? does not have a member named `isEmpty`
要打开它,请使用
(opt?)!.isEmpty
它将返回false
编辑:以澄清更多信息
var o1 = opt?
var o2 = ((opt?)?)
它自己什么也不做,只是给o1
,o2
分配相同的值,即字符串?。
要展开opt
和o1
,o2
都是可选的,需要单个代码>运算符以将其展开
另外,请不要误解String?
和opt?
它们在什么时候是不同的?在某些类型的取消公差后使用,使其成为可选的,当变量opt
后使用时,用于在可选链接中展开,并返回其返回的可选值
额外材料:
试着用这个来澄清一下
(((opt?)?)!).isEmpty //it will unwrap with single !
((((opt?)?)!)?).isEmpty //compiler will show suggestion to remove ?
下面的语句使optional of optional of optional变为optional
var opt:String??? = "Optional"
拆开
opt!!!.isEmpty
EDIT2
opt?
始终返回可选,但如果opt
定义为字符串!它是implicit optional
并且opt?
将返回optional(explicit)
。但是如果opt
已经是可选的opt?
将不起任何作用
来自swift编程指南
换言之:
If the type you are trying to retrieve is not optional, it will become optional because of the optional chaining.
If the type you are trying to retrieve is already optional, it will not become more optional because of the chaining.
因此:
If you try to retrieve an Int value through optional chaining, an Int? is always returned, no matter how many levels of chaining are used.
Similarly, if you try to retrieve an Int? value through optional chaining, an Int? is always returned, no matter how many levels of chaining are used.
是opt?.isEmpty
不同于String?
。它被称为。来自swift编程指南:
您可以通过在后面放置问号(?)来指定可选链接
要对其调用属性、方法或属性的可选值
如果可选项为非nil,则为下标。这与放置非常相似
可选值后的感叹号(!)可强制
展开其值。主要区别在于可选的链接
当可选值为nil时正常失败,而强制展开
当可选值为nil时触发运行时错误
这会根据您的想法创建可选选项
var-opt:String
现在
与除了相同
opt!.isEmpty //it will crash if opt is nil
如果opt代码>为nil
时,它不会在运行时崩溃。可选链用于在不调用的情况下调用可选的长序列。每个可选链返回可选的,即opt?返回可选的展开,如果为nil,则调用isEmpty,否则调用isEmpty和reutns值。
也
当您在写上面的语句时,它只是可选的(可选的不是可选的),由于大括号的原因,它无法展开。所以显示错误
$T2?? does not have a member named `isEmpty`
要打开它,请使用
(opt?)!.isEmpty
它将返回false
编辑:以澄清更多信息
var o1 = opt?
var o2 = ((opt?)?)
它自己什么也不做,只是给o1
,o2
分配相同的值,即字符串?。
要展开opt
和o1
,o2
都是可选的,需要单个代码>运算符以将其展开
另外,请不要误解String?
和opt?
它们在什么时候是不同的?在某些类型的取消公差后使用,使其成为可选的,当变量opt
后使用时,用于在可选链接中展开,并返回其返回的可选值
额外材料:
试着用这个来澄清一下
(((opt?)?)!).isEmpty //it will unwrap with single !
((((opt?)?)!)?).isEmpty //compiler will show suggestion to remove ?
下面的语句使optional of optional of optional变为optional
var opt:String??? = "Optional"
拆开
opt!!!.isEmpty
EDIT2
opt?
始终返回可选,但如果opt
定义为字符串!它是implicit optional
并且opt?
将返回optional(explicit)
。但是如果opt
已经是可选的opt?
将不起任何作用
来自swift编程指南
换言之:
If the type you are trying to retrieve is not optional, it will become optional because of the optional chaining.
If the type you are trying to retrieve is already optional, it will not become more optional because of the chaining.
因此:
If you try to retrieve an Int value through optional chaining, an Int? is always returned, no matter how many levels of chaining are used.
Similarly, if you try to retrieve an Int? value through optional chaining, an Int? is always returned, no matter how many levels of chaining are used.
是opt?.isEmpty
不同于String?
。它被称为。来自swift编程指南:
您可以通过在后面放置问号(?)来指定可选链接
要对其调用属性、方法或属性的可选值
如果可选项为非nil,则为下标。这与放置非常相似
可选值后的感叹号(!)可强制
展开其值。主要区别在于可选的链接
失败