Swift - 如何通过方法调用传递类型?

时间:2015-10-02 21:16:37

标签: ios swift swift2

领域对象:

class Dog: Object {
  dynamic var name = ""
  dynamic var age = 0
}

致电结果:

let results = realm.objects(Dog)

或者这样做:

let type = Dog.self
let results = realm.objects(type)

我希望能够通过这样的方法来实现:

class SomeClass {
  func callRealm(type: AnyObject) {
    let results = realm.objects(type)
  }
}

let someClass = SomeClass()
let type = Dog.self
someClass.callRealm(type)

我该怎么做?我对仿制药没有运气,尽管我认为这可能是要走的路。

1 个答案:

答案 0 :(得分:2)

您的功能callRealm应将输入设为AnyClass而不是AnyObject

func callRealm(type: AnyClass) {
    let results = realm.objects(type)
  }