如何在 Swift 中将类名指定为变量? [英] How do I specify class name as variable in Swift?
本文介绍了如何在 Swift 中将类名指定为变量?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想做这样的事情:
class Dog: Object {
dynamic var name = ""
dynamic var age = 0
}
let results = realm.objects(Dog)
...但将对象名称指定为变量.
...but specify the object name as a variable.
这不起作用:
let objectName = "Dog"
let results = realm.objects(objectName)
这两者都没有:
let object = Dog
let results = realm.objects(object)
...或者这个:
let object = Dog()
let results = realm.objects(object)
...或者这个:
let object: Dog
let results = realm.objects(object)
有什么想法吗?
推荐答案
可以直接用Dog.self
引用类型:
let type = Dog.self
我没有 Realm 的项目,但理论上你应该能够执行以下操作:
I don't have a project with realm, but in theory you should then be able to do something like:
let results = realm.objects(type)
这篇关于如何在 Swift 中将类名指定为变量?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文