Class Objects and Instance Variables in Objective-C

I find it difficult to turn around this concept. I will take a quote exactly from the book:

Class objects also inherit from classes located higher in the hierarchy. But since they do not have instance variables (only instances), they inherit only methods.

Correct me if I am wrong, but the class object will be as follows:

NSString *aString = [[NSString alloc]initWithString:@"abc.."]; 

The class object in this case is *aString - Still, am I right?

What confuses me is the second sentence in the quote above: "But since they do not have instance variables (only instances), they inherit only methods."

I thought the object (in this case *aString ) was an instance of the NSString class. The second sentence above means that the instance is something else. That makes no sense to me.

+4
source share
3 answers

You are wrong.

 NSString *aString = [[NSString alloc] initWithString:@"abc..."]; 

In this line we have left to right:

  • Type: NSString *
  • Variable Name: aString
  • the appointment
  • A Class : NSString
  • Class method call: +alloc
  • Call the instance method for the return value of the class method: -initWithString:
  • The object passed as a parameter to the instance method: @"abc..."

In Objective-C, a Class is actually a kind of object. You can interact with them in many ways that you can use, but since they are “classes”, they cannot have instance variables (because instance variables, by definition, apply only to instances). Classes have only methods. They inherit from other Classes , and that is how object inheritance is implemented.

For more information about this, check out this awful blog post from Greg Parker: http://www.sealiesoftware.com/blog/archive/2009/04/14/objc_explain_Classes_and_metaclasses.html

+6
source

In ObjectiveC, the classes themselves are objects.

In your example, aString is an NSString object. BUT NSString is also an object, it is a class object.

A class object has methods declared this way, for example

 @interface MyClass : NSObject { } +(void) myClassMethod; @end 

To call the myClassMethod method, type:

 [MyClass myClassMethod]; 

But there is no static variable, for example, in C ++ or Java, therefore a class object (here MyClass) cannot contain a variable, but an instance of the MyClass class can have a variable.

So, to resume NSString is an object of a class, and aString is an instance of an NSString.

+3
source

Objective-C has instances that are the objects you create and use, and there are (half-hidden) objects that are class objects that are created by the compiler. The class object where the methods for the class are stored; each instance contains only its own data (i.e. instance variables).

 Bob * myBob = [[Bob alloc] init]; 

Here myBob is an instance. Each instance refers to its class object. 1 When you call a method on an instance:

 [myBob frogBlastTheVentCore]; 

the runtime goes and searches for the method in the class object, and then uses the instance and instance data to execute the method. That the main organization of objects in Obj-C: instance objects stores data and has links to their class objects that contain methods. There is only one class object in a class; all instances of this class have a reference to the same class object.

A class (viewed as a "type" rather than an object for a moment 2 ) is said to be inherited from another class:

 @interface Bob : NSObject { NSColor * uniformColor; } + (BOOL) willShootAtPlayer; - (void) frogBlastTheVentCore; @end @interface VacuBob : Bob {} @end 

Here VacuBob is a subclass of Bob ; any VacuBob instance has its own uniformColor instance uniformColor . Similarly, an object of the VacuBob class created by the compiler is created; it also inherits from Bob - from an object of class Bob . This means that the VacuBob class VacuBob also has a willShootAtPlayer method.

In the line you posted:

 ... aString = [NSString alloc] ... 

the class object is actually NSString here. You call the class method with the name +[NSString alloc] 3 (class methods are denoted by + , not - 4 ). When the class name is used as the recipient of the message (the first half of the expression in parentheses), it refers to an object of class 5 . In this case, both NSString and aString are objects; these are just two different objects; aString is an instance.

Dave DeLong is connected with good mail on this (the chart, in particular, pretty much gives everything out); for more information, you should also read Matt Neuberg's description of The Secret Life of Classes in his iOS book. It describes the creation of class objects, their use, and other actions that they perform, in addition to retention methods.


1 This isa pointer: myBob->isa refers to an object of class Bob .

2 The variable related to the class object is of type Class . The type of the instance object is its class. Thus, the type Bob is equal to Class , and the type myBob is Bob * (that is, a pointer to an object of type Bob ). The distinction between the type of a variable and the type of an object can cause some confusion here.

3 The return value of alloc is an NSString instance that you invoke the initWithString: instance initWithString:

4 The methods of the parallel instance class are that they are called with the class object itself as an argument. Since class objects do not have their own data, the use of class methods is perhaps more limited than other OO languages; class methods are most often used for vending instances.

5 When it is used in a variable declaration: NSString * mySting; is the name of the variable type.

+1
source

All Articles