ios 如何在 Swift 中将对象归零
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/30612740/
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
How to Nil a object in Swift
提问by Muruganandham K
How to assign nil to an object in Swift. I'm getting error if assigned directly.
如何在 Swift 中将 nil 分配给一个对象。如果直接分配,我会收到错误。
回答by Icaro
nil cannot be used with nonoptional constants and variables. If a constant or variable in your code needs to work with the absence of a value under certain conditions, always declare it as an optional value of the appropriate type
nil 不能与非可选常量和变量一起使用。如果代码中的常量或变量在某些条件下需要在没有值的情况下工作,请始终将其声明为适当类型的可选值
also very importante to notice:
也非常重要的是要注意:
Swift's nil is not the same as nil in Objective-C. In Objective-C, nil is a pointer to a nonexistent object. In Swift, nil is not a pointer—it is the absence of a value of a certain type. Optionals of any type can be set to nil, not just object types
Swift 的 nil 与 Objective-C 中的 nil 不同。在 Objective-C 中,nil 是指向不存在对象的指针。在 Swift 中,nil 不是指针——它是某种类型的值的缺失。任何类型的 Optionals 都可以设置为 nil,而不仅仅是对象类型
I hope that helps you!
我希望对你有帮助!
.
.
回答by Santu C
Please check below -
请检查以下 -
var lastRecordedLocation: String?
lastRecordedLocation = nil
回答by iAnurag
To nil the object, it has to be declared with nil by ? mark. This means that the value can (possibly) be a nil. So declare it as :
要将对象归零,它必须用 nil by ? 标记。这意味着该值可以(可能)为零。所以将其声明为:
var lastRecordedLocation: String?
and then you can set it to nil
然后你可以将它设置为 nil
lastRecordedLocation = nil
回答by user3182143
NOTE : If you define an optional variable without providing a default value, the variable is automatically set to nil for you:
注意:如果您定义了一个可选变量而不提供默认值,则该变量会自动为您设置为 nil:
var lastRecordedLocation: String?
// lastRecordedLocation is automatically set to nil
or
lastRecordedLocation = nil
回答by ridvankucuk
That means your lastRecordedLocation
object is not Optional
type.
If you make lastRecordedLocation as Optional such as:
这意味着您的lastRecordedLocation
对象不是Optional
类型。如果您将 lastRecordedLocation 设为 Optional,例如:
var lastRecordedLocation:String?
You can set nil to that object later.
您可以稍后将 nil 设置为该对象。
回答by Leo
Only optional can be nil
只有可选可以为零
var lastRecordedLocation:CLLocation?
or this
var lastRecordedLocation:CLLocation!
var lastRecordedLocation:CLLocation?
或这个
var lastRecordedLocation:CLLocation!
回答by leng_1990
if you want nil the property , you need the Optionalvalue for the lastRecordedLocation
如果你想要 nil 属性,你需要lastRecordedLocation的Optional值
回答by Christian Ettelt
I used the following to empty a previously filled Data type variable
我使用以下内容清空以前填充的数据类型变量
var jsonData = Data()
The variable had been used for JSON serialisation before but was taking up too much memory, so I just reinitialised it and the size becomes 0 bytes again.
该变量之前曾用于 JSON 序列化,但占用了太多内存,所以我只是重新初始化了它,大小再次变为 0 字节。