[英]Semantics between optional and non-optional function parameters in Swift and best use cases
I've seen a lot of discussion regarding variables in Swift that are declared as var a: String?
我已经看到了很多有关在Swift中声明为var a: String?
变量的讨论var a: String?
and var a: String!
和var a: String!
the former being an optional variable of type String and the latter being implicitly unwrapped optional String. 前者是String类型的可选变量,而后者是隐式解包的可选String。 From my understanding, when something is implicitly unwrapped there's an assumption being made that there's a value already included in the optional value. 根据我的理解,当隐式解包某些东西时,就可以假设可选值中已经包含了一个值。 In order to use the syntax a!.toInt()
there would have to be an outside check like so: 为了使用语法a!.toInt()
,必须像这样进行外部检查:
if a != nil {
a!.toInt()
}
As a side note, it seems better practice to just declare variables as an optional type and then use optional chaining to unwrap the value if it exists. 附带说明一下,似乎更好的做法是将变量声明为可选类型,然后使用可选链解开该值(如果存在)。 Doing: 这样做:
if let numberString = a?.toInt() {
numberString.toInt()
}
Is there a scenario in where accessing a variable like a!.toInt()
should ever be used in practical applications? 在实际应用中是否应该存在访问a a!.toInt()
类的变量的情况? To me, it seems like you'd be asking for runtime errors. 在我看来,您似乎正在要求运行时错误。
Now for the actual question. 现在是实际问题。 I was looking through the documentation, specifically enumerateObjectsUsingBlock
on NSArray
and the block parameter is declared using an !
我正在查看文档,特别是NSArray
enumerateObjectsUsingBlock
,并且使用!
声明了block参数!
. 。 Does this mean that the writers of that function are assuming that the block parameter will be non-nil? 这是否意味着该函数的编写者假设block参数为非nil? If so, inside that function, what is the semantic difference between: 如果是这样,在该函数内部,以下两者之间的语义区别是什么?
func someMethodWithBlock(block:((String!)-> Void)!) {
var a = "Hello"
block.(a) //first syntax example
block?.(a)//2nd
block!.(a)//3rd
}
Implicitly Unwrapped Optionals are used almost everywhere in the Cocoa APIs as they are imported into Swift — this is to maintain compatibility with the kind of pointer semantics that are ubiquitous in Obj-C, without requiring !
隐式解包Optionals在将它们导入Swift时,几乎在Cocoa API中的所有地方都使用了-这是为了保持与Obj-C中普遍存在的那种指针语义的兼容性,而无需!
s and ?
s和?
s all over the place. 到处都是。 That's why the NSArray
method's block takes a String!
这就是为什么NSArray
方法的块采用String!
argument. 论点。
In Swift, regular Optionals ( Int?
, etc.) are largely preferable for the reasons you specified: they are less prone to errors. 在Swift中,由于您指定的原因,常规Optional( Int?
等)在很大程度上是可取的:它们不太容易出错。 Implicitly Unwrapped Optionals are mostly meant to be used only in certain cases, like when you need to pass self
to a function before initialization is complete . 隐式解包Optionals通常仅在某些情况下使用,例如当您需要在初始化完成之前将self
传递给函数时 。 (If you know a variable is not nil, unwrapping it like a!.toInt()
is just fine. The danger with IUOs is that it would be written a.toInt()
, and to a reader this looks completely safe, when in reality it's not.) (如果您知道一个变量不是nil,则将其像a!.toInt()
那样a!.toInt()
就可以了。IUO的危险在于它将被写入a.toInt()
,并且对于读者来说,当在事实并非如此。)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.