如何在Swift中删除一个对象 [英] How to Nil a object in Swift

查看:787
本文介绍了如何在Swift中删除一个对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何在Swift中为一个对象分配nil。如果直接分配,我会收到错误。

How to assign nil to an object in Swift. I'm getting error if assigned directly.

推荐答案

来自Apple文档:


nil不能与非可选常量和变量一起使用。如果代码中的
常量或变量需要在某些条件下缺少
值,请始终将其声明为相应类型的可选值

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

也非常重要注意:


Swift的零与Objective-C 中的nil不同。在Objective-C中,nil
是指向不存在的对象的指针。在Swift中,nil不是
指针 - 它缺少某种类型的值。任何类型的
的可选项都可以设置为nil,而不仅仅是对象类型

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中删除一个对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆