ClassName method?

I just watched a Stanford lecture on the iPhone since January 2010, and I noticed that the guy from Apple continues to refer to getting the class name of the objects using "className", for example.

NSArray *myArray = [NSArray arrayWithObjects:@"ONE", @"TWO", nil];
NSLog(@"I am an %@ and have %d items", [myArray className], [myArray count]);

However, I cannot get this to work, the closest I can come up with it is to use the class, is it wrong for className to delete it, just curious?

NSArray *myArray = [NSArray arrayWithObjects:@"ONE", @"TWO", nil];
NSLog(@"I am an %@ and have %d items", [myArray class], [myArray count]);

Gary

+3
source share
3 answers

The method classNameis available only on Mac OS X and not on iPhoneOS. It is commonly used to support scripts, so it does not exist on the iPhone. You can use something like:

NSString* className = NSStringFromClass([myArray class]);

. NSStringFromClass() , , , "" -.

+12

className Mac. iPhone NSObject:

//NSObject+ClassName.h
@interface NSObject (ClassName)

- (NSString *) className;

@end

//NSObject+ClassName.m
@implementation NSObject (ClassName)

- (NSString *) className {
  return NSStringFromClass([self class]);
}

@end

NSObject ( ) className.

+2
NSStringFromClass( [someObject class] );

and up to Objective-C 2.0

someObject->isa.name
0
source

Source: https://habr.com/ru/post/1748877/


All Articles