Home objective-c What is the difference between a class instance from the class object?

What is the difference between a class instance from the class object?

Author

Date

Category

What is the difference between a class instance from the class object in Objective-C?
They asked such a question on the interview, I replied in bewilderment “I do not understand the question, isn’t it the same thing?”.
Please not to kick much, I taught the Ozh-Si myself, at an accelerated pace, and there was no time to deal with the Concept itself.


Answer 1, Authority 100%

I think you got under the distribution due to the insufficient qualifications of the interviewing … & nbsp; if you can engage in direct translation terms, then you can learn a lot of interesting things :).

The OBJ-C documentation uses the terms Class Object and Class Instance . If they are literally translated into Russian, it will turn out exactly what you were asked about. Moreover, in Russian, naturally the class object read it instance . While perfectly obviously, which means the English term class object – this object in which is stored, so to speak, class information.

Class Objects

alass definition contains various
Kinds of Information, MUCH OF IT ABOUT
Instances of the Class:

  • The Name of the Class and Its Superclass
  • A Template Describing A Set Of Instance Variables
  • The Declarations of Method Names and Their Return and Argument Types
  • The Method Implementations

This Information IS Compiled and
Recorded in Data Structures Made
Available to the Runtime System. Their
Compiler Creates Just One Object, A
Class Object , to Represent The Class.
The Class Object Has Access to All The
Information ABOUT THE CLASS, Which
Means Mainly Information About What
Instances of the Class Are Like. IT’s.
Able to Produce New Instances
According to the Plan Put Forward in
The Class Definition.

Although A Class Object Keeps The
PROTOTYPE OF A Class Instance ,
it’s not an instance itself . IT HAS NO Instance Variables of Its Own and
IT CAN’T Perform Methods Intended for
Instances of the class. However, A.
Class Definition Can Include Methods
INTENED SPECIFICALLY FOR THE CLASS
Object-Class Methods AS Opposed To
Instance Methods. A Class Object.
Inherits Class Methods from the
CLASSES ABOVE IT IN THE HIERARCHY,
JUST AS Instances Inherit Instance
Methods.


Answer 2, Authority 12%

If approaching a purely utilitarian point of view, then in Objective-C you can define two types of methods for any class:

  1. class methods – begin with +, sometimes called singleton – in Russian and do not crowd out. The essence of them is that it does not need to create an instance of the class for their call.

Example can serve, for example, [Uicolor GreyColor ]. Objects obtained in this way do not require subsequent release of memory management mechanisms. In some cases, the use of such methods is very convenient.

  1. Methods of an instance of an object. – Their announcement begins with -. These are classic methods that work only after you have created an object with Alloc.

Probably, it was about this that you were asked on an interview: -)


Answer 3, Authority 6%

The class object contains all static properties, and the class instance contains all non-static properties. The class object is initialized when accessing the class or when creating an instance, therefore this design [Uicolor GreyColor] is possible. A class instance is initialized only when it is created.
I will add more:

In the instance methods “-” compiler for us adds the variable this (reference to the instance), and in the methods of the class method>+ "(in other languages ​​they are called static ) He does not do this, so inside it can not turn to the instance.


Answer 4

struct objc_class {
  STRUCT OBJC_CLASS * ISA;
  STRUCT OBJC_CLASS * SUPER_CLASS;
  Const Char * Name;
  Long Version;
  Long info;
  long instance_size;
  STRUCT OBJC_IVAR_LIST * IVARS; 
#if Defined (Release3CompatibilityBuild)
   STRUCT OBJC_METHOD_LIST * METHODS;
#ELSE.
   STRUCT OBJC_METHOD_LIST ** METHODLISTS;
#Endif.
   STRUCT OBJC_CACHE * CACHE;
   STRUCT OBJC_PROTOCOL_LIST * PROTOCOLS;
};

Probably, it will be clear what is meant under the class object. http://www.opensource.apple.com/source/objc4/ OBJC4-208 / Runtime / Objc-class.h


Answer 5

no different. These are synonyms.
The “template” or “object description” is a class, and the “object” represents an “instance” of this class.
Source

Programmers, Start Your Engines!

Why spend time searching for the correct question and then entering your answer when you can find it in a second? That's what CompuTicket is all about! Here you'll find thousands of questions and answers from hundreds of computer languages.

Recent questions