Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Class type Objective C

In the NSObject protocol, it defines a method that is similar to this:

-(Class) class

What type of object is the Class object? Or is it even an object? What can I do with the object? Can I get the base class or adopted protocols?

like image 669
Richard J. Ross III Avatar asked Aug 21 '10 21:08

Richard J. Ross III


People also ask

What are objective classes?

A course objective specifies a behavior, skill, or action that a student can demonstrate if they have achieved mastery of the objective. As such, objectives need to be written in such a way that they are measurable by some sort of assessment. Course objectives form the foundation of the class.

How do you declare a class variable in Objective-C?

Variable Declaration in Objective-C You will use extern keyword to declare a variable at any place. Though you can declare a variable multiple times in your Objective-C program but it can be defined only once in a file, a function or a block of code.

How do I create an instance of a class in Objective-C?

There is basically one way to do this: you send a class the alloc message. The alloc class method is implemented by the NSObject class, the root class from which all other classes inherit. It causes memory to be set aside for the instance so that an instance pointer can point to it.

What are methods in Objective-C?

Basically in Objective-C, we call the function as method. The Objective-C foundation framework provides numerous built-in methods that your program can call. For example, method appendString() to append string to another string.


1 Answers

Class is itself a class defined by the Objective-C runtime, akin to the Class class in Java. For example, you can use the function class_getClassName() to get the name of a class:

NSObject *o = [[[NSObject alloc] init] autorelease];
NSLog(@"%s\n", class_getClassName([o class]));  // prints "NSObject"

You can do all kinds of introspection/reflection with Class objects; see the Objective-C runtime reference for details.

like image 115
Adam Rosenfield Avatar answered Nov 12 '22 14:11

Adam Rosenfield