OC- basic summary (1)

OC basic summary

Looking back on these basic knowledge, have a new understanding of many knowledge points, with a solid foundation to grow faster.


OC program source file suffix is.M m on behalf of the essage message mechanism. Main is still the entry and exit of the OC program, the main function has a return value of type int, representing the end of the program.

#import preprocessor directive, is an enhanced version of the #inlcude directive, the role of the contents of the file in the pre compiled copy to the command. #import has been optimized, the same file no matter how many times #import, will only contain one.
brief principle: #import command in the file contains the time, the bottom will first determine whether the file is included, if it will be skipped over.

Therefore, the main difference between the #import #include is to use the #include need to deal with repeated references, and #import can prevent the same file is contained many times, do not need to deal with repeated references.


  1. A feature set Apple or third party in advance of a member in the development process often used to write the function in advance. These functions are encapsulated in a class or function.
  2. Foundation framework
    Foundation: framework, this framework provides some of the most basic functions, input and output, some data types.

How to use object oriented programming

Object oriented maintenance and modification is very convenient. When we meet a need, don’t do it ourselves.

  1. Take a look at whether there are ready-made people who are specialized in doing this, the framework, if there is direct use.
  2. If you do not have to create a function of the object, and the creation of the object can be used for many times.

Classes and objects

  1. Object – specific
    object is a concrete existence in real life, visible, tangible, take over can be used directly
  2. Class
    Like attracts like. collectively, Birds of a feather flock together. A general
    class has the same characteristics or behavior of a group of things and abstract, cannot be used directly, if you have to use the words, specific only to find classes in the class, also is the object, and then use the.

Relationships between classes and objects

Class is a template, the object of the class is created based on this template, what kind of template in the class, there is nothing in the object, can not be more, it can not be less.

How to design a class

Three elements of design class

  1. Class name.
  2. This kind of thing has the same characteristics, what kind of things with the hand.
  3. What can such things do.

Class loading

  1. When creating an object, it is necessary to access the.
  2. Declaring a pointer to a class can also access a class.

When the program is running, the class will be stored in the code segment of the memory when the class is accessed for the first time.

The class is loaded only when the class is first accessed, and once the class is loaded into the code segment, it will not be released until the end of the program.

How objects are stored in memory.

Example: Person *p1 = [Person new];

  1. Person *p1; in the stack memory to apply for a space in the stack memory to declare 1 Person type pointer variable p1. P1 is a pointer variable that can only store addresses.
  2. [person new]; really create the object in memory is actually the code.
  3. The new method creates a space of appropriate size in heap memory, and then creates an object in the space according to the template of the class. What attributes are defined in the
    class template, these attributes are declared in the object. Another object in the
    object, called ISA, is a pointer to the address of the class in which the object belongs.
  4. Initialize the properties of the object and assign the default values to the properties of the object.
    if the type of the property is the basic data type, then the value 0
    if the attribute type is a pointer type in C language, then assigned to NULL
    if the type of attribute for the OC language class pointer type, then the value for the nil
  5. Note that
    1. Only attributes in the object have no method, the attributes include their own class attributes, plus a isa pointer to the class in the code segment.
    2) how to access the properties of an object, the pointer name -> the property name
    according to the pointer to find the pointer to the object, the object to find the property to access.
    3). How to call a method. [name of the name of the name];
    first according to the name of the pointer to find the object, the object found to call the method, in accordance with the ISA pointer to the object to find the class. Then call the methods in the class.
    4) why not store the method in an object.
    because each object code is not necessary as like as two peas, for each object to save a method, so it is a waste of space, since all the same, just save a copy in the code segment.
    5). The object property has a default value.

The difference between nil and NULL

NULL can be used as a pointer variable value, if the value of a pointer variable is the NULL value of the pointer does not point to any memory space, in fact equivalent to 0. NULL is actually a macro, that is 0.

Nil can only be used as the value of the pointer variable, representing the pointer variable without pointing to any space in memory. In fact, nil is equivalent to 0, but also a macro, that is, 0.

So NULL and nil are actually the same, although the use of NULL can be used in place of nil, but it is not recommended to use. C pointer with NULL OC class pointer with nil.

Person *p1 = nil; indicates that the P1 pointer does not point to any object.
if the value of a pointer to nil represents the pointer does not point to any object, this time if the P1 pointer to access the P1 pointer pointing to the object’s properties, the operation will be wrong. If you call the object through the P1 pointer, the operation will not be wrong, but the method does not perform.

Multiple pointers to the same object

Pointer variables of the same type can be assigned to each other. P1, P2 points to the same object, no matter who modifies the properties of the object will be modified. Because they point to the same memory space.

Objects and methods

Object can be used as a parameter of a method or as a return value of a method. The essence of the
class is a data type that we define, and the size of the object in memory is determined by ourselves

When the object is passed as a method parameter, the address is passed. Therefore, the value of the object pointed to by the argument variable is affected by the parameter in the method. Object is the return value of the method, and the address of the object is returned

Object as a class attribute.

The nature of an attribute is a variable, and when the object is created, the properties of the object are created one by one in accordance with the requirements of the class template. What types of attributes are in a class template, and what are the properties of the object.
if the property of an object is an object of another class, this property is just a pointer variable, and no object is generated. This time, but also for the property to assign an object’s address, can be used normally. What is the type of attribute in the
class template, and what is the type of attribute in the object.

Declaration and invocation of a class method

The call to a class method does not depend on the object. If you want to call a class method, you do not need to create an object. Instead, you can call the class method directly using the class name.

Calling procedure for class method and object method.

OC- basic summary (1)
class method and object method call process

The class method saves space and is efficient because the calling class method does not need to create objects. However, the property cannot be accessed directly in a class method. Because the property is created only in the object when the object is created, and the class method may not have a class object at the time of execution, the property cannot be accessed. But we can create class objects in a class method.

Similarly, it is not possible to call other methods of the current class directly through self, because the object method can only be called by an object and can be called directly in the object method.

Therefore, if the method does not require direct access to the property, and does not need to call other object methods directly, then we can define it as a class method directly.


  1. Subclasses are inherited from the parent class, which means that the subclass owns all the members of the parent class, including properties and methods.
  2. Inheritance is the inheritance of a class, not an object, and the same object as the parent object.

If not all of the subclasses have the method, then this method should not be defined in the parent class, because once defined in the parent class, then all subclasses have the method

Inherited characteristics

  1. A class can have only one parent class and cannot have more than one parent class.
  2. Transitivity, A inherit B, B inherit C then A will have both B and C members.


The NSObject class is the superclass of all classes, and the NSObject class contains methods for creating objects, so the classes we create must be inherited directly or indirectly from the NSObject. The
NSObject has the properties of a isa pointer, so that each subclass object has a pointer called isa.

Super keyword

  1. The subclass already has the properties of the parent class, which is equivalent to the attribute of the parent class that has been defined in the subclass, so that there is no attribute in the subclass that has the same name as the parent class.
  2. You can use the super keyword to call an object method that the current object inherits from the parent class. You can use self or super
  3. Class methods can also be inherited class. Subclasses can directly call the parent class method.
  4. Super can only be used to call a parent’s object method or a class method and cannot be used to access attributes.
  5. Subclasses are inherited from the parent class, equivalent to all the members of the parent class template in the subclass template.
  6. To create a subclass object, it is still based on the sub class template to create the object, but the subclass template has the properties and methods of the parent class, but also has its own attributes and methods.
  7. The parent method is more readable with super, and we will soon know that this method is the parent class method.

Access modifiers:

Is used to modify properties that limit the properties of an object in that range.

@private: private, modified attributes can only be accessed internally in this class. The protected
@protected: modified property can only be accessed in this class as well as subclasses of this class. Can only be accessed in a method implementation of this class and subclass.
@package: is a property that can be accessed in the current framework.
@public: public, its modified properties can be accessed anywhere.

If you do not specify an access modifier for the property default: the protected
subclass can still inherit the private property of the parent class. Even if the parent class property is private, it is not possible to directly access the private property inherited from the parent class, but can be accessed by the set get method.

The scope of
access modifiers began to write from the access modifiers, until another access modifiers or end bracket, all the attributes of the middle of the application of the access modifiers.

The use of the proposed
@public no matter what circumstances do not use, do not directly to the outside world.
@private if the property is only intended to be used in this class, it is not intended to be used in a subclass.
@protected if you want the property to be used only in this class and subclasses of this class.

Access modifiers can only be used to modify attributes and cannot be used to modify methods.

Substitution principle

Subclasses can replace the location of the parent class, and the function of the program is not affected.
means that a parent class pointer points to a subclass object that can call the method and properties of a subclass.

: the Liskov Substitution Principle a pointer can not only store the address of the object, you can also store the subclass object address
if the type of a pointer is NSObject type, then the pointer can be stored in any OC object.
if the type of the element of an array is a OC pointer type, the array can not only store the class object, but also store the subclass object.
if an array element is a NSObject pointer type, it means that any type of object can exist in an array.
if a method parameter is an object, we can pass this class object can also pass a subclass object
when a pointer to the parent class 1 subclass object, called a subclass object in the parent class members through the parent class pointer can.


If the class has a parent class, but the class behavior and different parent class. This can be done by overriding the parent class.

When a parent pointer points to a subclass object, the method that is called by the parent class pointer, if overridden in a subclass object.


Refers to the same behavior, for different things have different forms of expression. The same behavior with a variety of forms. Subclasses override the parent class by polymorphism.

Description method

The description method is defined in NSObject. We rewrite the description method to modify the output of NSLog. NSLog is the bottom of the description method.

Similarities and differences between structures and classes

The same point:
can be more than one data package as a data


  1. The structure can only encapsulate data, and the class can not only encapsulate data but also encapsulate behavior.
  2. Structure variables are allocated in the stack space (local)
  3. Object variables are allocated in heap space
  4. Stack features: space is relatively small, but stored in the stack of data access efficiency is higher
  5. Heap features: space is relatively large, but the efficiency of data access is relatively low.

If an entity is not represented, only the attribute.
so if the property is relatively small, only a few, then this time is defined as the structure, allocated in the stack, improve efficiency. If the property is more, do not define the structure, because the structure of the variable will occupy a larger space in the stack, resulting in reduced access efficiency.

Class nature

Class is stored as a Class object in the code segment

The five regional
stack local variable
memory stack allows the programmer to apply for their own space, need global and static variables data segment
BSS control section uninitialized global variables to memory storage has been initialized, static variables are the constants of the
code used to store code program.

Class load: when the class is accessed for the first time, the class is loaded into the code segment.

  1. When the class is loaded into the code segment
    class is accessed for the first time, the class is loaded into the code segment store, which is the class load.
  2. What kind of class is stored in the code segment.
    any data that is stored in memory has a data type, and any space applied in memory has its own type. So what type of space is stored in the code segment?
    stored in the code segment class
    1). The first step to create a Class object in the code segment, Class is a kind of Foundation framework, the Class object is used to store the information.
    2). The class information is stored in the Class object in
    so is stored in the form of Class object in code, the Class object storage class is called the class object, so the class object storage class also has a isa pointer, a pointer to a parent class object.
  3. Once the class is loaded into the code segment, when will it be released. Once the
    class is loaded into the code segment, it will not be recovered unless the program ends.
  4. How do I get the class object stored in the code segment?
    1) the class method class that calls the class can get the address of the class object of the storage class.
    2) the object method class of the object is called to get the address of the class object to which the object belongs.
    3) the value of the ISA pointer in the object is actually the address of the class object in the code segment.
    4). The class object get storage class, completely equivalent to
    Class C1 = [Person class]; C1 is Person, C1 is equivalent to the Person, can be used method to call the class object.
    note: when you declare a Class pointer, you don’t need to add the * * * * * * * * * * * * * when typedef
  5. How to use the
    object class 1). Get the class object storage class, Class C1 = [Person class]; C1 object is the Person class, the C1 object is equivalent to the Person
    2). Methods using the class object to call the class.
    3) you can use the class object to call the new method to create an object that is stored in a class object.
    4) using a class object, you can only call the class method of the class because the class object is equivalent to the class in which it exists
  6. Summary
    1) classes are stored in the code segment in the form of Class objects.
    2) class objects can be used to call the class methods of the class.
    3) through the class method to get the storage class object.

SEL selector

In fact, SEL is a class, to be stored in the memory space to store data, SEL object is used to store a method. The
class is stored in the code segment in the form of a Class object. So how do you store methods in class objects?

  1. Create a SEL object
  2. Store the method information in this SEL object
  3. The SEL object is stored as an attribute of the Class object.
  4. Similar to the form of an array of all SEL objects will be stored, that is, the list of methods. Information stored in a SEL object.

How to get the storage method of the SEL object

  1. Because SEL is a typedef type, it has been added at the time of customization. So we don’t need to add a pointer to the SEL pointer.
  2. SEL object to store method SEL sel = @selector (sayHi);
  3. Call the essence of the method: [p1 sayHi];
    1). First get the sayHi method of storage SEL object, which is to get the SEL data stored in the sayHi method, SEL message.
    2) send the SEL message to the P1 object.
    3) after the P1 object receives the SEL message, you know that you want to call the method
    4. To find the class object of the storage class based on the ISA pointer of the object.
    5) to find a way to match the incoming SEL data in this class object after the object is found. If you do, if you do not find the parent class, until NSObject.

The 1 most important mechanisms of OC: the message mechanism, the essence of the calling method is to send the SEL message to the object, [p1 sayHi], which means sending sayHi messages to the P1 object.

Manually send SEL messages to objects

  1. First get the SEL data.
  2. Send the SEL message to the P1 object. Through the method of
    – (ID) performSelector: (SEL) aSelector; Person *p1 = [Person manually send messages new]; SEL S1 = @selector (sayHi); [p1 performSelector:s1]; [p1 and sayHi] effect is exactly the same. The method with a / / / / and two parameters. If there are multiple parameters to package parameters in an object, the object passed – (ID) performSelector: (SEL) aSelector withObject: (ID) object; (ID) – performSelector: (SEL) aSelector withObject: (ID) object1 withObject: (ID) object2;

Point grammar

Accessing object properties using point syntax.

Syntax: the name of the object. The name of the attribute p.name = @ Jack @ is removed; this time it will assign @ Jack @ to the _name property of the P object.

is not the essence of the @ Jack directly assigned to the _name attribute of the P object, in essence, is called the set method.

When you use the point syntax assignment, the compiler converts point syntax to the code that calls the setter method.
when using point syntax values, the compiler converts point syntax to the code that calls the getter method.

In the getter setter method with some grammar, can cause infinite recursion and program crashes.
is mainly to see the situation, the get method if the grammar is called set method can be used.
for example, we can use lazy loading point syntax for assignment, because lazy loading is the get method, and the assignment is called set method, suobu not recursive calls.
if the property does not encapsulate setter getter is not able to use the point syntax.


@property automatically generated getter setter method declaration.
principle: automatically generated by the compiler at compile time.

Implementation of automatic generation of getter setter method in @synthesize
@synthesize age / / age @property statement must be in front of the.

  1. Generates a true private property, the type of the property and the corresponding @property type @synthesize, the name of the property and the @synthesize corresponding to the @property name.
  2. Implementation of automatic generation of setter method. In a way that is automatically assigned to the private property that is automatically generated. Self-> age = age;
  3. Implementation of automatic generation of getter method. Returns the value of the generated private property

@synthesize do not want to generate private property, setter getter implementation of the operation we have written the property on it.

@synthesize @property name = existing property name; @synthesize age = _age;
  1. No private property will be generated
  2. Direct implementation of the realization of the realization of the setter setter: the value of the value assigned to the specified parameters directly to the getter. Getter implementation: returns the value of the specified property directly.

@property enhancement.

After Xcode 4.4, just write a @property compiler will automatically generate private attributes, and automatically generate getter setter declaration and implementation.

@property NSString *name;

  1. Automatically generates a private property, the type of the attribute is consistent with the @property type, the name of the property is the same as the name of the @property, and the name of the property is automatically underlined.
  2. The declaration and implementation of the setter getter method for automatically generating this property. The value of the parameter is assigned directly to the automatically generated private property, and the value of the generated private property is returned directly.

@property generates the set get method and member properties, but if you override the setter getter method at the same time, the private property will not be generated automatically. Need to write.
parent class property can be inherited as a quilt class, but the generated property is private, can be accessed through the setter getter method

Dynamic and static types

OC is a weak language, the compiler at compile time, grammar check is not so strict.
strong type of language: the compiler at compile time, do grammar check, it is very strict, the line is OK, not is not.

Static type
refers to a pointer to an object that is a class object.
dynamic type
a pointer to an object is not an object of this class.

Compile check

At compile time, the compiler checks whether a pointer can be used to call a pointer to an object.

Judgment principle: look at the type of pointer is there in this method, if you think that can be called, the compiler through, if not in this class, then the compiler error. This is called the compiler check, at compile time, can call the object method is mainly to see the type of pointer, we can make the type of pointer conversion, to achieve the purpose of cheating the compiler.

Operation check

Compile check just fool the compiler, but this method is able to perform, so running at run time to check to check whether the object will really have on the implementation of this method, if there is, if there is no Baocuo

Compiler, the compiler will be C code OC code into a binary, the compiler is a software, apple wrote their own called LLVM, you can compile C OC Swift language.

We can through the following methods to judge whether the object is the first method, if there is to go, if not, do not go to avoid the program in the wrong way when there is no error.

1). Whether this method can execute judgment object. - (BOOL) respondsToSelector: (SEL) aSelector; (2) the most common). If there is a specified class method to determine the class. + (BOOL) instancesRespondToSelector: (SEL) aSelector; 3). To determine whether the specified object for the specified class or object subclass. - (BOOL) isKindOfClass: (Class) aClass; 4). To determine whether the object is the object of the specified class does not include subclasses. - (BOOL) isMemberOfClass: (Class) aClass; 5). To determine whether the class for 1 other subclasses. + (BOOL) isSubclassOfClass: (Class) aClass;


All base classes of the class OC, according to the LSP NSObject pointer can point to any OC object, all NSObject pointer is a pointer to the OC object
universal, can point to any disadvantage: if you want to call to the unique way of subclass object, it must do type conversion.

ID pointer

Is a universal pointer that can point to any OC object

  1. ID is a typedef custom type
  2. ID pointer, is a total of 1 pointers, you can point to any OC object.
    1). ID is the 1 typedef custom type that has been added at the time of the definition. So, when you declare a ID pointer, you don’t need to add any more.
    2) the ID pointer is the 1 universal pointer, any OC object can refer to
  3. Similarities and differences between NSObject and ID.
    the same point: Universal pointer, you can perform any OC object.
    : when method calls to go through the NSObject pointer, the compiler can compile check when method calls to the ID pointer type, the compiler directly through the call, no matter what method. Note: the ID pointer can only call the method of the object, and cannot use the point syntax. If you use the point syntax, the compiler error. If you want to declare 1 pointers do not use NSObject but use ID

Instancetype — ID

The difference between ID and instancetype

  1. Instancetype can only be used as a method return value and cannot be used elsewhere.
    ID can declare pointer variables, can also be used as a parameter, can also be used as a return value.
  2. Instancetype is the 1 type of object that represents the current class.
    ID is a 1 type of pointer, just 1 addresses, no type of pointer.

New method

To create an object, we preceded the new method by using the
class name * pointer name = class name new];
new is actually a 1 class method, which acts as:

  1. Create object.
  2. Initialization object.
  3. Returns the address of the object.

The interior of the new method, in fact, is the first call to the alloc method, called the init method. The
alloc method is a 1 class method. Role: the 1 classes call this method to create the object of that class, and return the object, the allocation of memory space. The
init method is the 1 object method. Role: initialize object.

Init method
function: initialize the object, the object of the initial value of the property, the init method is called the construction method. What the
init method does: initialize the object and assign the default value to the object’s properties.
if the attribute type is the basic data type is assigned to 0, C pointer assignment NULL, OC pointer assignment nil.
so, we create 1 objects that do not have a default value for the attribute of the object if it is not assigned to the object’s properties.

Override the init method specification:
1). You must first call the parent class’s init method, and then assign the method’s return value to self.
2) initialization of an object by calling the init method is likely to fail, and if initialization fails, the return is nil.
3) to determine whether the parent class is initialized successfully, to determine whether the value of self is nil, if not for the success of the nil declaration.
4) initialize the properties of the current object if it succeeds.
5) returns the value of self at last.

Custom construction method:
1). The return value of the custom construction method must be instancetype.
2) the name of the custom construction method must begin with initWith.
3) the implementation of the method is the same as that of init.

If there is something wrong with the welcome. I am xx_cc, a guy who has grown up for a long time but hasn’t got enough for two.