ios - 如何在 Swift 中对对象进行 Nil

标签 ios swift cocoa-touch null

如何在 Swift 中将 nil 赋值给一个对象。如果直接分配,我会收到错误消息。

enter image description here

最佳答案

From Apple Documentation:

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’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

希望对你有帮助!

关于ios - 如何在 Swift 中对对象进行 Nil,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30612740/

相关文章:

iphone - 模态呈现的 TableView 没有导航栏

ios - React-native - 由于双重转换导致构建失败

iphone - libz.1.dylib 框架的 Apple Mach-O 链接器错误

swift - 决定性进度指标

ios - 按照维护顺序快速解析 json

ios - 创建目录和调用 setResourceValue 的正确位置在哪里?

ios - 如何在 iOS 客户端中传递 AWS AppSync 自定义请求 header ?

iphone - 键盘上推阻塞 TextView 的设计模式

iOS 13performActionForshortcutItem 现在不会在启动时在 SceneDelegate 中调用,而是在应用程序启动后调用。为什么?

objective-c - NSHTTPCookie 拒绝删除