Reputation: 13262
I have a subclass of Object
in Realm called BrowserCategory
, and Artist
, Decade
and Genre
are subclasses of BrowserCategory
.
Right now each subclass of BrowserCategory
has this LinkingObject
property songs
:
let songs = LinkingObjects(fromType: Song.self, property: "decade")
(for the Decade
subclass, for example)
I'd like to write this in the BrowserCategory
base class so I've tried this:
var songs = LinkingObjects(fromType: Song.self, property: className().lowercased())
However this returns the base class name and not the subclass name, giving the following error:
- Property 'Song.browsercategory' declared as origin of linking objects property 'Decade.songs' does not exist
Is there a way to do this the way I want to?
Upvotes: 2
Views: 1159
Reputation: 534950
You are probably looking for type(of:self)
, which is polymorphic, as this demonstration illustrates:
class BrowserCategory {
func className() -> String {
return String(describing:type(of:self))
}
}
class Artist : BrowserCategory {}
class Decade : BrowserCategory {}
class Genre : BrowserCategory {}
let a = Artist()
a.className() // "Artist"
I should caution you, however, that to ask for a class's name as a string is a very odd thing to do. It is more usual to work with a class type as a type.
Upvotes: 2