Skip to content

Instantly share code, notes, and snippets.

@denkeni
Last active August 29, 2015 14:24
Show Gist options
  • Save denkeni/34c41b58ec0313490918 to your computer and use it in GitHub Desktop.
Save denkeni/34c41b58ec0313490918 to your computer and use it in GitHub Desktop.
Swift Syntax Design Complexity

Check Class/Type

Objective-C

What you need to know are isKindOfClass: and class:

A *a = [[A alloc] init];
Class AClass = [A class];

BOOL isAClass1 = [a isKindOfClass:[A class]];
BOOL isAClass2 = [a isKindOfClass:AClass];

Swift

What you need to know is only is in most cases. But if you'd like to store Type in a variable/property for use...

let a = A()
let AType = A.self

let isAClass1 = (a is A)
let isAClass2 = (a.dynamicType === AType)

For the second line let AType = A.self, it is equivalent to:

let AType : AnyClass = A.self   // more precisely
let AType : A.Type = A.self    // even more precisely

In a nutshell, when is doesn't work, think it as instance.dynamicType === class.self

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment