问题
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