Objective-c Class and Object summary

Source: Internet
Author: User

Objective-c class and Object summary:

header files. h files: (header file)

Declaration of class: @interface class Name: Name of the parent class

Conventions for class names: 1, capital letters, such as person

Parent class: Usually NSObject, we can also specify the parent class ourselves.

The declaration of the class is @end as a terminator.

source files. m file (source file): The specific implementation of the properties, methods.

The implemented structure:

Header file #import "xxx.h"//Import class declaration

@implementation class Name

..... properties and methods of the implementation of the specific ....

@end//with @end as Terminator

Member variables:

The characteristics of an entity

Statement:

The member variable must be included in the curly brace weight

@interface Class Name: Parent class name

{

Inside this curly brace, declare the member variable

}

@private, @protected, @public to control the accessibility of member variables (access rights)

@private Private member = = only allows current class access

@protected protected member = = "Allows access to the current class and subclass.

The default is protected (when declaring member variables, do not specify the default is @protected)

@public public Members = = = "All classes are accessible.

Conventions for member variables:

OBJC, all variables of the object type must use pointers, that is, variables other than the basic data type (Int,char,float,double,bool).

For example: A class of entities, structs, arrays.

In OBJC, whether a custom class or a system class object must be a pointer

Once a class has been defined, we instantiate it with two steps:

1), allocating memory = = = "Alloc

2), initialize the entity = = = "Init"

Recommended instantiation of the wording:

Class name * variable name =[[class name alloc] init];

Method Classification:

1) static method with + definition

2) dynamic method with-definition

To define the format of a method:

No parameters:

+/-(return value type) method name;

When there is a parameter, a colon is required before the argument:

+/-(return value type) method name: (type of parameter) parameter name;

If there are more than one parameter, we will add a parameter description before the second parameter, so that we could quickly understand the meaning of the parameters in the method when we call.

+/-(return value type) method name: (type of parameter) parameter name description of the second parameter: (the type of the second parameter) the name of the second parameter ....

If it is not declared in. h, but is defined directly in. m, the method is private and cannot be accessed externally. Private methods are only allowed to be called in the current source file. Other classes cannot be accessed.

The difference between calling a static method and a dynamic method:

static method: Only call with class name is allowed

Dynamic methods: Only allowed inside the class (directly in the source file of the class) or by instantiating an object to invoke

Properties: They are usually implemented through the corresponding setter or Getter method.

Conventions: Setter methods are typically added with a Set,getter method in front of the property name (usually using the property name without a Get)

OBJC's Point syntax:

Pointer variable name. Property name

The point syntax is specific to whether the setter method or the getter method is called, depending on whether the current operation is an assignment or a read

The Assignment (=) is called by the setter method, which is called when the Getter method is read.

@property and @synthesize Keywords:

Our code for defining attributes is basically consistent, and we use @property and @synthesize to automatically generate getter, setter methods.

    • If you declare only one property A and do not use the @synthesize implementation: The compiler uses _a as the member variable of the property (if no member variable is defined _a automatically generates a private privated member variable _a, and if the member variable _a is defined, the custom member variable _a is used. Note: If the member variable defined at this time is not _a but a then a member variable _a is automatically generated, which has no relation to the custom member variable a);
    • If an attribute A is declared, implemented using @synthesize a , but the implementation does not specify a member variable to use (for example, birthday above): Then the compiler will use a as a member variable of the property (if member variable A is defined). The custom member variable is used, and if it is not defined at this time, a private member variable A is automatically generated, noting that if _a is defined at this time it has no relation to the generated a member variable);
    • If an attribute A is declared and implemented using the @synthesize a=_a , the procedure has specified the member variable used: The specified member variable is used as the property variable;

Self keyword: You can not only indicate that the current object can also represent the class itself

Self can invoke dynamic methods and static methods

The essence of self: pointer = = = This pointer is pointing to the current caller.

Self represents the caller of the current method

Construction Method:

The default construction method is init, which inherits from the NSObject class.

We can use our own method of construction (parameter transfer, etc.)

ID Type:

The ID type can represent all types (either their own defined classes or system-defined classes), and in most cases, a pointer.

The ID type is run to determine the specific type.

Where to apply the ID: Usually when you return an instantiated object (pointer) of a class

Super Keyword: represents the parent class.

When you use the Super keyword, you call the method of the parent class directly.

Even if the subclass overrides the same method, super calls the method of the parent class.

Description Method:

Defined in NSObject

When we use the%@ output, we call the description method. (where the use of%@ is not limited to nslog)

Override the description method, usually in a certain format, by agreeing to a member variable or property of a class.

Rewritten description, strictly prohibits direct printing of self (causing a dead loop)

By default, if we do not override the description method, the output is the class name and address, for example, the person outputs "<Person:0x100202310>".

Inherited:

Inheritance can be recursive: for example, b inherits from C,a and can inherit from B a===>b====>c

OBJC does not allow multiple inheritance: When we declare a class, it is not allowed to write the names of multiple classes after the colon, because OBJC can only allow single integration.

Subclass: The parent class member variable can be accessed directly (not allowed by private), property

Subclasses can call public methods of the parent class

Subclasses can also replicate the public methods of the parent class.

member variables for subclasses and parent classes:

The child class and the parent class member variable are independent, the entity of the child class modifies the value of the member variable, does not have the parent class, the parent class modifies the value of the member variable, and does not affect the child class. Subclasses and parent classes, even if the member variables have an inheritance relationship, they are all independent.

Class Call Method:

First, in the current class, find the called method, and if the current class implements the method being called, the method of the current class is called directly.

If the current class does not implement the called method, the program automatically looks up the immediate parent class, and if the called method is defined within the parent class, the method of the parent class is called.

If the parent does not find the method being called, the program will look up again and search for NSObject.

Finally, if you find NSObject (OBJC base class), there is no definition of the method, this time the compiler will error.

Using the Super keyword, the method in the immediate parent class of this class is called. If the called method is not found in the parent class, the program finds the parent class of the ancestor until NSObject.

Macro definition:

#define macro Definition name string (does not need to be wrapped with @ "")

Function: In the place where the macro is defined, the compiler automatically copies the entire string to the right of the macro definition name.

BOOL Type: The value has two yes and no, essentially bool is a value of unsigned char,yes is the value of 1,no is 0.

In OC, when logical comparisons are made (if statements, etc.) are also subject to the C language specification:

A value other than 0 is true and a value of 0 is false.

SEL variables:

SEL: is the unique ID of the method. Method IDs of the same name are the same.

There are several ways we can get the value of the SEL variable:

1. @selector (method name)

2, Nsselectorfromstring (@ "method name")

The format of the method is called through the SEL variable:

[Object Performselector:sel variable withobject: Parameter 1 withobject: Parameter 2];

function pointers:

A pointer variable that points to a function.

There are two ways of defining a function pointer.

1, similar to the C language function pointer

2. Using IMP, when defining a function pointer using IMP, you need to set the compile parameters of Xcode: Enable strict Checking of objc_msgsend calls to No, the call can be compiled correctly.

Class Type:

The definition of a class is obtained by a normal string.

After a class-type variable has been defined, it is assumed that our class variable is a known class.

You can instantiate an object, make a method call, and so on.

1 class variable name = [Class or object class];
2 class variable name = [Class or object superclass];
3 Class Variable name = nsclassfromstring (string of method name);

Special methods:

[Object ClassName]: Returns the class name of the object

[Object Respondstoselector:sel variable name]: Determines whether the object has a function that the SEL variable points to. If there is no SEL variable pointing to this function, the return value is no

Objective-c Class and Object summary

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.