ios 问号“?”是什么意思 在迅速?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/24057171/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-31 00:30:48  来源:igfitidea点击:

What the meaning of question mark '?' in swift?

iosswift

提问by Rajneesh071

In Swift programming I found some question marks with objects.

在 Swift 编程中,我发现了一些关于对象的问号。

var window: UIWindow?

Can anybody explain the use of it?

有人可以解释一下它的用途吗?

采纳答案by Salman Zaidi

You can use ifand lettogether to work with values that might be missing. These values are represented as optionals. An optionalvalue either contains a value or contains nilto indicate that the value is missing. Write a question mark (?) after the type of a value to mark the value as optional.

If the optional value is nil, the conditional is falseand the code in braces is skipped. Otherwise, the optional value is unwrapped and assigned to the constant after let, which makes the unwrapped value available inside the block of code.

您可以使用iflet一起处理可能丢失的值。这些值表示为optionals。一个optional值要么包含一个值,要么包含nil来指示该值丢失。在值的类型后写一个问号 (?) 以将该值标记为optional

如果可选值为nil,则条件为false并且跳过大括号中的代码。否则,可选值被解包并分配给常量 after let,这使得解包值在代码块内可用。

Excerpt From: Apple Inc. “The Swift Programming Language.” iBooks. https://itun.es/pk/jEUH0.l

摘自:Apple Inc. “The Swift Programming Language”。电子书。https://itun.es/pk/jEUH0.l

For Example:

例如:

var optionalString: String? = "Hello"
optionalString == nil

var optionalName: String? = "John Appleseed"
var greeting = "Hello!"
if let name = optionalName {
    greeting = "Hello, \(name)"
}

In this code, the output would be Hello! John Appleseed. And if we set the value of optionalNameas nil. The ifconditional result would be falseand code inside that ifwould get skipped.

在此代码中,输出将为Hello! John Appleseed. 如果我们将值设置optionalNamenil. 在if有条件的结果将是false和里面的代码if会得到跳过。

回答by Cezar

Question marks after a type refer to Optionals, a way in Swift which lets you indicate the possibility that a value might be absent for any type at all, without the need for special constants.

类型引用后的问号是OptionalsSwift 中的一种方式,它允许您指示任何类型可能根本不存在值的可能性,而无需特殊常量。

It's used in the same situations you'd explicitly return nilin Objective-C, when there is no object to be returned, or for values that are not objects, constants such as NSNotFound. Optionals provide a consistent way of achieving this across all data types.

它用于与您return nil在 Objective-C 中明确使用的情况相同的情况,当没有要返回的对象时,或者对于不是对象的值,诸如NSNotFound. Optionals 提供了一种在所有数据类型中实现这一点的一致方式。

From the Apple provided iBook

来自 Apple 提供的 iBook

You use optionals in situations where a value may be absent. An optional says:

  • There is a value, and it equals x

or

  • There isn't a value at all

Here's an example. Swift's String type has a method called toInt, which tries to convert a String value into an Int value. However, not every string can be converted into an integer. The string "123" can be converted into the numeric value 123, but the string "hello, world" does not have an obvious numeric value to convert to.

在值可能不存在的情况下使用可选项。一个可选的说:

  • 有一个值,它等于 x

或者

  • 根本没有价值

这是一个例子。Swift 的 String 类型有一个名为 toInt 的方法,它尝试将 String 值转换为 Int 值。但是,并非每个字符串都可以转换为整数。字符串 "123" 可以转换为数值 123,但是字符串 "hello, world" 没有明显的数值可以转换。

let possibleNumber = "123"
let convertedNumber = possibleNumber.toInt()
// convertedNumber is inferred to be of type "Int?", or "optional Int"

Because the toInt method might fail, it returns an optional Int, rather than an Int. An optional Int is written as Int?, not Int. The question mark indicates that the value it contains is optional, meaning that it might contain some Int value, or it might contain no value at all. (It can't contain anything else, such as a Bool value or a String value. It's either an Int, or it's nothing at all.)

因为 toInt 方法可能会失败,所以它返回一个可选的 Int,而不是一个 Int。可选的 Int 写为 Int?,而不是 Int。问号表示它包含的值是可选的,这意味着它可能包含一些 Int 值,或者它可能根本不包含任何值。(它不能包含任何其他内容,例如 Bool 值或 String 值。它要么是 Int,要么什么都没有。)

There is a whole section on the language reference iBook on Optionals, and they are mentioned several times throughout the book. You should have a thorough look at it, since it's a fundamental concept of Swift programming, and one that is not prevalent in many other languages.

iBook on Optionals 有一整节关于语言参考,并且在整本书中多次提到它们。您应该彻底了解它,因为它是 Swift 编程的一个基本概念,并且在许多其他语言中并不普遍。

回答by Kumar KL

the optional Value of the type .

类型的可选值。

For Ex : the optional version of the type casting operator : as?,mean asuse for the type casting with might be optional to cast .

例如:类型转换运算符的可选版本:as?,意味着as用于类型转换的可能是可选的。