objective-c - 将 Swift 值类型与对象(即运行时的引用类型)区分开来?

标签 objective-c swift

有没有什么方法可以在运行时使用 Swift 或 Objective-C 运行时区分值类型(例如元组、枚举和结构,包括基元)和对象,也称为 Swift 中的引用类型?

最佳答案

这是一种检查变量是否是类实例的方法:

object_isClass(type(of: yourVariable))

它使用 type(of:) , 它返回变量的动态类型。

这里有一些测试:

let a = 1

let b = ("a", 2.3)

enum MyEnum { case one; case two }
let c = MyEnum.one

struct MyStruct { let property: Int }
let d = MyStruct(property: 0)

class MyClass {}
let e = MyClass()

object_isClass(type(of: a))  //false
object_isClass(type(of: b))  //false
object_isClass(type(of: c))  //false
object_isClass(type(of: d))  //false
object_isClass(type(of: e))  //true

关于objective-c - 将 Swift 值类型与对象(即运行时的引用类型)区分开来?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53136371/

相关文章:

json - 如何在 Swift 中将 Realm 对象转换为 JSON?

ios - Swift - 计算属性返回

iphone - 如何使用 Youtube API (GTLYoutube) 在 iPhone sdk 上搜索视频?

ios - 将 ECSlidingViewController 与 TableView 一起使用

objective-c - 尝试打开(创建)SQLite d/b时出错(“EXC_BAD_ACCESS”)

ios - 如何在 uiscrollview 中从最后一个 View 滚动到第一个 View

ios - RestKit Router - 如何使用嵌套变量实现路由?

ios - 在 Swift 中画线

swift - Vapor 一次上传多个文件

ios - Swift 中的错误,无法分配给文字值