> On 12 Sep 2016, at 14:27, Quincey Morris > <quinceymor...@rivergatesoftware.com> wrote: > > On Sep 12, 2016, at 00:08 , Gerriet M. Denkmann <gerr...@mdenkmann.de> wrote: >> >> I want to do: >> if self.dynamicType == SomeClass { … } >> >> But the compiler won’t accept this (or any number of variations thereof). > > Try this: > > if self is SomeClass { … } > > Typically, if you need to use a class in an expression, you’ll need to refer > to the metatype instance: > > if self.dynamicType == SomeClass.self { … } > > but this would be an exact match. If you want the semantics of “isKindOf” > (that is, the object is of the specified class or a subclass) then “is” is > the way to go.
if self.dynamicType == SArray.self { print("Test1 \(self.dynamicType) is a subclass of SArray") } else { print("Test1 \(self.dynamicType) is NOT subclass of SArray") } prints: Test1 SArray2 is NOT subclass of SArray Not quite what I expected. But this works: if self.dynamicType == SArray1.self || self.dynamicType == SArray2.self || self.dynamicType == SArray3.self { print("Test2 \(self.dynamicType) is a subclass of SArray") } else { print("Test2 \(self.dynamicType) is NOT subclass of SArray") } prints: Test2 SArray2 is a subclass of SArray The only problem: It is kind of fragile. When I create a new class SArray5 then I must remember to fix this test. I would much prefer to have the semantics of “isKindOf”. Kind regards, Gerriet. _______________________________________________ Cocoa-dev mailing list (Cocoa-dev@lists.apple.com) Please do not post admin requests or moderator comments to the list. Contact the moderators at cocoa-dev-admins(at)lists.apple.com Help/Unsubscribe/Update your Subscription: https://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com This email sent to arch...@mail-archive.com