使用Swift字符串的可选链接 [英] Optional chaining with Swift strings
问题描述
如果有Swift变量,则使用可选链接
With optional chaining, if I have a Swift variable
var s: String?
可能包含nil或包装在Optional中的字符串.因此,我尝试这样做以延长其长度:
s might contain nil, or a String wrapped in an Optional. So, I tried this to get its length:
let count = s?.characters?.count ?? 0
但是,编译器希望这样做:
However, the compiler wants this:
let count = s?.characters.count ?? 0
我对可选链接的理解是,一旦开始在点分表达式中使用?.
,其余的属性将变为可选属性,并且通常由?.
而不是.
访问.
My understanding of optional chaining is that, once you start using ?.
in a dotted expression, the rest of the properties are made optional and are typically accessed by ?.
, not .
.
所以,我进一步挖掘了一下,然后在操场上尝试了这个
So, I dug a little further and tried this in the playground:
var s: String? = "Foo"
print(s?.characters)
// Output: Optional(Swift.String.CharacterView(_core: Swift._StringCore(_baseAddress: 0x00000001145e893f, _countAndFlags: 3, _owner: nil)))
结果表明s?.characters
确实是Optional实例,表明s?.characters.count
应该是非法的.
The result indicates that s?.characters
is indeed an Optional instance, indicating that s?.characters.count
should be illegal.
有人可以帮助我了解这种情况吗?
Can someone help me understand this state of affairs?
推荐答案
当您说:
我对可选链接的理解是,一旦开始在点分表达式中使用
?.
,其余的属性将变为可选属性,并且通常由?.
而不是.
访问.
My understanding of optional chaining is that, once you start using
?.
in a dotted expression, the rest of the properties are made optional and are typically accessed by?.
, not.
.
我会说你快到了.
并非所有属性都是可选的,而是原始调用是可选的,因此其他属性看起来都是可选的.
It’s not that all the properties are made optional, it’s that the original call is optional, so it looks like the other properties are optional.
characters
不是可选属性,也不是count
,但是您调用它的值是可选的.如果有一个值,则characters
和count
属性将返回一个值;否则,将返回一个值.否则,返回nil
.因此,s?.characters.count
的结果返回了Int?
.
characters
is not an optional property, and neither is count
, but the value that you are calling it on is optional. If there is a value, then the characters
and count
properties will return a value; otherwise, nil
is returned. It is because of this that the result of s?.characters.count
returns an Int?
.
如果这两个属性中的任何一个都是可选的,则需要向其中添加?
,但对于您而言,则不是.所以你不用.
If either of the properties were optional, then you would need to add ?
to it, but, in your case, they aren’t. So you don’t.
在评论后进行了编辑
从评论中:
s?.characters.count
和(s?.characters)?.count
都可以编译,但(s?.characters).count
不能编译,这仍然很奇怪.为什么第一个和最后一个表达式有区别?
I still find it strange that both
s?.characters.count
and(s?.characters)?.count
compile, but(s?.characters).count
doesn't. Why is there a difference between the first and the last expression?
我会在这里尝试回答,那里的空间比注释字段中的空间还多:
I’ll try and answer it here, where there is more room than in the comment field:
s?.characters.count
如果s
为nil,则整个表达式返回nil
,否则返回Int
.因此,返回类型为Int?
.
If s
is nil, the whole expression returns nil
, otherwise an Int
. So the return type is Int?
.
(s?.characters).count // Won’t compile
这很糟糕:如果s
是nil
,则(s?.characters)
是nil
,因此我们无法在其上调用count
.
Breaking this down: if s
is nil
, then (s?.characters)
is nil
, so we can’t call count
on it.
为了调用(s?.characters)
上的count
属性,该表达式需要可选地展开,即写为:
In order to call the count
property on (s?.characters)
, the expression needs to be optionally unwrapped, i.e. written as:
(s?.characters)?.count
已编辑以进一步添加
我能解释的最好的方法是使用以下游乐场代码:
The best I can get to explaining this is with this bit of playground code:
let s: String? = "hello"
s?.characters.count
(s?.characters)?.count
(s)?.characters.count
((s)?.characters)?.count
// s?.characters.count
func method1(s: String?) -> Int? {
guard let s = s else { return nil }
return s.characters.count
}
// (s?.characters).count
func method2(s: String?) -> Int? {
guard let c = s?.characters else { return nil }
return c.count
}
method1(s)
method2(s)
这篇关于使用Swift字符串的可选链接的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!