Arjun
Arjun

Reputation: 89

IOS: Universal way to get type of class

Is there a universal way to get the type of class of an object such as:

NSString *classType = [Class kind];

rather than iterating through them like:

if ([object isKindOfClass: [NSNull Class]]) {
//it is null
}
else if ([object isKindOfClass: [NSObject Class]]) {
//it is an object
}

//and so forth

Upvotes: 1

Views: 55

Answers (2)

Chris Trahey
Chris Trahey

Reputation: 18290

While it is common to use NSStringFromClass(anObj), you should also know about Swift 3's type(of:) function.

import Foundation
class Foo:NSObject {}
let bar = Foo()
let barType = type(of:bar)
print("\(barType)")

Looks like this in playgrounds:

enter image description here

Upvotes: 0

Owen Hartnett
Owen Hartnett

Reputation: 5935

NSString * NSStringFromClass(Class aClass);

Upvotes: 1

Related Questions