07-18-2023, 07:41 PM
I know that I can check the type of a var in Swift with `is`
if item is Movie {
movieCount += 1
} else if item is Song {
songCount += 1
}
but how can I check that two instances have the same class? The following does not work:
if item1 is item2.dynamicType {
print("Same subclass")
} else {
print("Different subclass)
}
I could easily add a "class" function and update it in each subclass to return something unique, but that seems like a kludge...
if item is Movie {
movieCount += 1
} else if item is Song {
songCount += 1
}
but how can I check that two instances have the same class? The following does not work:
if item1 is item2.dynamicType {
print("Same subclass")
} else {
print("Different subclass)
}
I could easily add a "class" function and update it in each subclass to return something unique, but that seems like a kludge...