简体   繁体   English

Swift和最佳用例中可选和非可选函数参数之间的语义

[英]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.

相关问题 在 swift 中使用“默认值”创建非可选 Codable 的最佳方法 - Best approach to create Non-optional Codable with `default values` in swift 可选和非可选不能从返回可选的函数中返回 - Optional and non-optional not able to be returned from function that returns an optional 在Swift中使用.Success枚举返回非可选 - Return non-optional with .Success enum in Swift 迅速:推迟非可选对象的初始化 - swift: defer non-optional object initialization 不能在“任何”SWIFT类型的非可选值上使用可选链接 - cannot use optional chaining on non-optional value of type 'Any' SWIFT 快速使用非可选对象将其视为可选 - Using non-optional object while swift consider it optional Swift中的非可选类型不应该包含可选吗? - Shouldn't an optional be inclusive to a non-optional type in Swift? 是否可以动态创建swift协议的可选和非可选版本? - Is it possible to create an optional and non-optional version of a swift protocol dynamically? 不能对“ViewController”类型的非可选值使用可选链接 - Cannot use optional chaining on non-optional value of type 'ViewController' 有没有办法强制Swift中的TextField中的文本是非可选的? - Is there a way to force the text in a TextField in Swift to be non-optional?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM