I know what "?" and "!" mean when I declare variables in Swift. But what do they mean when using these variables? For example, in this code:
我知道什么“?”和“!”当我在Swift中声明变量时。但是在使用这些变量时它们意味着什么?例如,在此代码中:
var attachment: NSTextAttachment = NSTextAttachment()
attachment.image = UIImage(named: "placeholder.png")
attachment.image!.size ... // or attachment.image?.size ...
What's the difference between attachment.image!.size
and attachment.image?.size
?
attachment.image!.size和attachment.image?.size之间有什么区别?
3 个解决方案
#1
35
Use attachment.image!.size
if you're guaranteed that image?
isn't nil
. If you're wrong (and it is nil
) your app will crash. This is called forced unwrapping.
如果你保证图像,请使用attachment.image!.size?不是零。如果你错了(并且它是零)你的应用程序将崩溃。这称为强制展开。
If you're not sure it won't be nil
, use image?
.
如果您不确定它不会为零,请使用图像?
In your case image!
is OK, since you control whether placeholder.png
exists.
在你的情况下图像!没关系,因为你可以控制是否存在placeholder.png。
Read all of the documentation on Swift Optionals. It's a basic and fundamental part of the language.
阅读有关Swift Optionals的所有文档。它是该语言的基本和基本部分。
#2
18
what is ! and ?:
什么是 !和?:
- Use ? if the value can become nil in the future, so that you test for this.
- 使用 ?如果将来该值变为零,那么您可以对此进行测试。
- Use ! if it really shouldn't become nil in the future, but it needs to be nil initially.
- 使用 !如果它真的不应该在未来变为零,但它最初需要为零。
if You are using attachment.image!.size
any variable with ! means, compiler don't bother about that variable has any value or nil
. it goes to take action further, it will here try to get size of image. if attachment.image
is nil
then app will be crash here.
如果你正在使用attachment.image!.size任何变量!意思是,编译器不打扰该变量有任何值或为零。它进一步采取行动,它将尝试获得图像的大小。如果attachment.image为nil那么应用程序将在这里崩溃。
While, attachment.image?.size
, this will make sure you, if attachment.image
isn't nil then further action will be take place, otherwise But it confirm Application will not be crash in case of nil
value of image
.
虽然,attachment.image?.size,这将确保你,如果attachment.image不是nil那么将进行进一步的操作,否则它确认应用程序不会在图像的nil值的情况下崩溃。
Summary:
概要:
We should have to use !, when we make sure option type variable has value at a time and we need to take action on it further, Otherwise.
我们应该使用!,当我们确保选项类型变量一次具有值时,我们需要对其进行进一步操作,否则。
#3
1
When you are using !
and the variable is nil
it will trigger run time error but if use ?
it will fail gracefully. More detail information in apple document
当你使用!并且变量为nil会触发运行时错误但是如果使用?它会优雅地失败。苹果文档中的更多详细信息
#1
35
Use attachment.image!.size
if you're guaranteed that image?
isn't nil
. If you're wrong (and it is nil
) your app will crash. This is called forced unwrapping.
如果你保证图像,请使用attachment.image!.size?不是零。如果你错了(并且它是零)你的应用程序将崩溃。这称为强制展开。
If you're not sure it won't be nil
, use image?
.
如果您不确定它不会为零,请使用图像?
In your case image!
is OK, since you control whether placeholder.png
exists.
在你的情况下图像!没关系,因为你可以控制是否存在placeholder.png。
Read all of the documentation on Swift Optionals. It's a basic and fundamental part of the language.
阅读有关Swift Optionals的所有文档。它是该语言的基本和基本部分。
#2
18
what is ! and ?:
什么是 !和?:
- Use ? if the value can become nil in the future, so that you test for this.
- 使用 ?如果将来该值变为零,那么您可以对此进行测试。
- Use ! if it really shouldn't become nil in the future, but it needs to be nil initially.
- 使用 !如果它真的不应该在未来变为零,但它最初需要为零。
if You are using attachment.image!.size
any variable with ! means, compiler don't bother about that variable has any value or nil
. it goes to take action further, it will here try to get size of image. if attachment.image
is nil
then app will be crash here.
如果你正在使用attachment.image!.size任何变量!意思是,编译器不打扰该变量有任何值或为零。它进一步采取行动,它将尝试获得图像的大小。如果attachment.image为nil那么应用程序将在这里崩溃。
While, attachment.image?.size
, this will make sure you, if attachment.image
isn't nil then further action will be take place, otherwise But it confirm Application will not be crash in case of nil
value of image
.
虽然,attachment.image?.size,这将确保你,如果attachment.image不是nil那么将进行进一步的操作,否则它确认应用程序不会在图像的nil值的情况下崩溃。
Summary:
概要:
We should have to use !, when we make sure option type variable has value at a time and we need to take action on it further, Otherwise.
我们应该使用!,当我们确保选项类型变量一次具有值时,我们需要对其进行进一步操作,否则。
#3
1
When you are using !
and the variable is nil
it will trigger run time error but if use ?
it will fail gracefully. More detail information in apple document
当你使用!并且变量为nil会触发运行时错误但是如果使用?它会优雅地失败。苹果文档中的更多详细信息