• Open Menu Close Menu
  • Apple
  • Shopping Bag
  • Apple
  • Mac
  • iPad
  • iPhone
  • Watch
  • TV
  • Music
  • Support
  • Search apple.com
  • Shopping Bag

Lists

Open Menu Close Menu
  • Terms and Conditions
  • Lists hosted on this site
  • Email the Postmaster
  • Tips for posting to public mailing lists
Re: How to do isKindOfClass in Swift
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: How to do isKindOfClass in Swift


  • Subject: Re: How to do isKindOfClass in Swift
  • From: "Gerriet M. Denkmann" <email@hidden>
  • Date: Mon, 12 Sep 2016 14:50:25 +0700

> On 12 Sep 2016, at 14:27, Quincey Morris <email@hidden> wrote:
>
> On Sep 12, 2016, at 00:08 , Gerriet M. Denkmann <email@hidden> 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 (email@hidden)

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:

This email sent to email@hidden


  • Follow-Ups:
    • Re: How to do isKindOfClass in Swift
      • From: Quincey Morris <email@hidden>
References: 
 >How to do isKindOfClass in Swift (From: "Gerriet M. Denkmann" <email@hidden>)
 >Re: How to do isKindOfClass in Swift (From: Quincey Morris <email@hidden>)

  • Prev by Date: Re: Progress Indicator
  • Next by Date: Re: Why doesn't this crash?
  • Previous by thread: Re: How to do isKindOfClass in Swift
  • Next by thread: Re: How to do isKindOfClass in Swift
  • Index(es):
    • Date
    • Thread