Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Description to return just ClassName?

The default description for a class instance returns "ClassName: 0x105120". How might I modify the method below to just return the "ClassName"?

// The code below just returns the address ...
- (NSString *)description {

 NSString *result;

 result = [NSString stringWithFormat:@"%p", self];
    return result;
}

EDIT: in that case would this be correct? Although I do understand that if I want to actually get the className as an NSString I should use NSStringFromClass([self class])

- (id)init {
 NSLog(@"_init: %@", [self class]);
 [super init];
 return self;
}

thanks in advance -gary-

like image 229
fuzzygoat Avatar asked Sep 09 '09 14:09

fuzzygoat


People also ask

What does className return?

It returns the Class object that represents the specified class name. This is used if you need to get the Class object. This roughly corresponds to . getClass() which returns the Class object that corresponds to the object instance.

Can a return type be a class name?

Returning a Class or InterfaceA method can have the class name as its return type. Therefore it must return the object of the exact class or its subclass. An interface name can also be used as a return type but the returned object must implement methods of that interface.

How do I get a className?

The simplest way is to call the getClass() method that returns the class's name or interface represented by an object that is not an array. We can also use getSimpleName() or getCanonicalName() , which returns the simple name (as in source code) and canonical name of the underlying class, respectively.


1 Answers

iPhoneOS: NSStringFromClass([self class])
MacOS: [self className]

... gives you an NSString with the class name

Edit:

For both iPhoneOS and MacOS the way to go is:

NSStringFromClass([self class])

like image 126
Nikolai Ruhe Avatar answered Nov 03 '22 08:11

Nikolai Ruhe