How to check what class belongs AnyObject?

倾然丶 夕夏残阳落幕 提交于 2019-12-12 04:07:00

问题


For example, there are two models 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 = ""
    }

And there is a certain function

func test(object: AnyObject) {

}

When a call is transferred as an argument 'realm.objects(Music)[0]'

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

Can I check in function, the object belongs to a class 'Music' or to a 'DownloadMusic'


回答1:


Try this:

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


来源:https://stackoverflow.com/questions/36899820/how-to-check-what-class-belongs-anyobject

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!