如何检查哪个类属于AnyObject?

时间:2016-04-27 20:03:57

标签: ios swift realm

例如,有两种模型Realm Information

    import Foundation
    import RealmSwift

    class Music: Object {

        dynamic var id = ""
        dynamic var title = ""
        dynamic var url = ""
    }
    class DownloadMusic: Object {

        dynamic var id = ""
        dynamic var title = ""
        dynamic var path = ""
    }

并且有一定的功能

func test(object: AnyObject) {

}

当呼叫作为参数转移时#realm.objects(音乐)[0]'

let realm = try! Realm()    
test(realm.objects(Music)[0])

我可以签入功能,该对象属于一个类'音乐'或者下载音乐'

1 个答案:

答案 0 :(得分:2)

试试这个:

func test(object: AnyObject) {
    if object is Music {
        print("music")
    } else if object is DownloadMusic {
        print("downloadmusic")
    }
}