This question already has an answer here:
这个问题已经有了答案:
- What is an optional value in Swift? 12 answers
- Swift中的可选值是什么?12个答案
In swift programming i found some question marks with objects.
在快速编程中,我发现了一些带有对象的问号。
var window: UIWindow?
Can anybody will explain the use of it?
有人能解释一下它的用途吗?
3 个解决方案
#1
50
You can use
if
andlet
together to work with values that might be missing. These values are represented asoptionals
. Anoptional
value either contains a value or containsnil
to indicate that the value is missing. Write a question mark (?) after the type of a value to mark the value asoptional
.您可以使用if和let来处理可能丢失的值。这些值表示为选项。可选值要么包含一个值,要么包含nil以表明该值丢失。在将值标记为可选的值之后,写一个问号(?)
If the optional value is
nil
, the conditional isfalse
and the code in braces is skipped. Otherwise, the optional value is unwrapped and assigned to the constant afterlet
, which makes the unwrapped value available inside the block of code.如果可选值为nil,条件为false,括号中的代码被跳过。否则,可选值将被打开并分配给let之后的常量,这使得在代码块中可以使用未包装的值。
Excerpt From: Apple Inc. “The Swift Programming Language.” iBooks. https://itun.es/pk/jEUH0.l
摘自:苹果公司“Swift编程语言”。“iBooks。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 optionalName
as nil
. The if
conditional result would be false
and code inside that if
would get skipped.
在此代码中,输出将是Hello!约翰苹果子。如果我们把optionalName的值设为nil。if条件结果将是false,而在if内部的代码将被跳过。
#2
15
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.
类型后面的问号表示选项,这是Swift中的一种方式,它允许您指出任何类型都可能根本不存在值,而不需要特殊常量。
It's used in the same situations you'd explicitly return nil
in 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.
在同样的情况下,你可以在Objective-C中显式地返回nil,当没有对象要返回时,或者对于非对象的值,比如NSNotFound这样的常量。Optionals提供了在所有数据类型中实现此功能的一致方法。
From the Apple provided iBook
苹果提供iBook
You use optionals in situations where a value may be absent. An optional says:
在某个值可能不存在的情况下,您可以使用选项。一个可选的说:
- There is a value, and it equals x
- 有一个值,它等于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.
这是一个例子。Swift的字符串类型有一个名为toInt的方法,它试图将字符串值转换为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值,或者根本不包含任何值。(它不能包含任何其他内容,比如Bool值或字符串值。要么是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.
关于Optionals的语言参考iBook有一个完整的部分,在全书中有几次提到。你应该仔细看看它,因为它是Swift编程的基本概念,而且它在许多其他语言中并不流行。
#3
1
the optional Value of the type .
类型的可选值。
For Ex : the optional version of the type casting operator : as?
,mean as
use for the type casting with might be optional to cast .
对于Ex:类型转换操作符的可选版本:as?,表示对类型强制转换的使用可以选择强制强制转换。
#1
50
You can use
if
andlet
together to work with values that might be missing. These values are represented asoptionals
. Anoptional
value either contains a value or containsnil
to indicate that the value is missing. Write a question mark (?) after the type of a value to mark the value asoptional
.您可以使用if和let来处理可能丢失的值。这些值表示为选项。可选值要么包含一个值,要么包含nil以表明该值丢失。在将值标记为可选的值之后,写一个问号(?)
If the optional value is
nil
, the conditional isfalse
and the code in braces is skipped. Otherwise, the optional value is unwrapped and assigned to the constant afterlet
, which makes the unwrapped value available inside the block of code.如果可选值为nil,条件为false,括号中的代码被跳过。否则,可选值将被打开并分配给let之后的常量,这使得在代码块中可以使用未包装的值。
Excerpt From: Apple Inc. “The Swift Programming Language.” iBooks. https://itun.es/pk/jEUH0.l
摘自:苹果公司“Swift编程语言”。“iBooks。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 optionalName
as nil
. The if
conditional result would be false
and code inside that if
would get skipped.
在此代码中,输出将是Hello!约翰苹果子。如果我们把optionalName的值设为nil。if条件结果将是false,而在if内部的代码将被跳过。
#2
15
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.
类型后面的问号表示选项,这是Swift中的一种方式,它允许您指出任何类型都可能根本不存在值,而不需要特殊常量。
It's used in the same situations you'd explicitly return nil
in 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.
在同样的情况下,你可以在Objective-C中显式地返回nil,当没有对象要返回时,或者对于非对象的值,比如NSNotFound这样的常量。Optionals提供了在所有数据类型中实现此功能的一致方法。
From the Apple provided iBook
苹果提供iBook
You use optionals in situations where a value may be absent. An optional says:
在某个值可能不存在的情况下,您可以使用选项。一个可选的说:
- There is a value, and it equals x
- 有一个值,它等于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.
这是一个例子。Swift的字符串类型有一个名为toInt的方法,它试图将字符串值转换为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值,或者根本不包含任何值。(它不能包含任何其他内容,比如Bool值或字符串值。要么是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.
关于Optionals的语言参考iBook有一个完整的部分,在全书中有几次提到。你应该仔细看看它,因为它是Swift编程的基本概念,而且它在许多其他语言中并不流行。
#3
1
the optional Value of the type .
类型的可选值。
For Ex : the optional version of the type casting operator : as?
,mean as
use for the type casting with might be optional to cast .
对于Ex:类型转换操作符的可选版本:as?,表示对类型强制转换的使用可以选择强制强制转换。