Objective-c language-description methods and Sel-bolck and protocols

Source: Internet
Author: User
Tags print object

First, description method

The description method includes class methods and object methods. (Included in the NSObject Class)

(i) Basic knowledge

-description (Object method)

When you use NSLog and @% to output an object, the object's description method is called and the return value is returned for output.

+description (class method)

When you use NSLog and @% to output an object, the description method of the class object is called, the return value is returned for output, the entire object is printed once, and the print object uses%@.

Use @% to print objects such as ("@%", P) The default printout is the < class name: memory address, although the string is also an object, but the string is special when printing with @%.

So how do you implement all the properties of the printed object? Override the description method in the implementation of the class.

(ii) Implement all properties of the Print object

(c) Difference

The +description method determines the output of the class object, which is the class itself

The-description method determines the output of the instance object, which is the object created by the person.

(iv) Printing related supplements

Second, SEL

SEL: Full Name selector represents the location where the method is stored.

How is the method stored in memory?

Person *p=[[person alloc] init];

[P test];

The process of finding a method:

(1) First, the test method name is packaged into SEL type data;

(2) Find the corresponding method address according to the SEL data;

(3) Call the corresponding method according to the method address.

(4) Note: During this operation, there is a cache, the first time to find a time to find, very consumption performance, and then use the time when it is used directly.

About _cmd: There is a-cmd inside each method that represents the current method.

Note: The sel is actually a wrapper around the method, wrapping the method into an SEL type of data, finding the corresponding method address, and then calling the method after locating the method address. These are run-time features, and the message is to send the SEL, and then find the address based on the SEL and invoke the method.

Block and protocol

First, Bolck

(a) Introduction

What is block? Apple's recommended type, high efficiency, saves code in the run. Used to encapsulate and save code, a bit like a function, block can be executed at any time.

Bolck and Function Similarity: (1) can save the Code (2) has the return value (3) The physical parameter (4) calls the same way.

identifier ^

(ii) Basic use

(1) define the block variable

Int (^sumblock) (int,int);//parameter, return value type int

void (^myblock) ();//no parameter, return value type is empty

(2) using block encapsulation code

  

(3) block access to external variables

1) External variables can be accessed within the block;

2) By default, external local variables cannot be modified inside the block

3) Add the __block keyword to the local variable, the local variable can be modified inside the block.

(4) Use typedef to define block types (much like pointers to functions)

Typedef Int (^myblock) (int, int);

You can then use this type to define the block variable.

Myblock A, B;

a=^ (int a,int b) {return a-A;};

Myblock b2=^ (int n1,int n2) {return n1*n2;};

Ii. Protocol (agreement)

(a) Introduction

1.Protocol: For a single use, declare a lot of methods (cannot declare member variables), cannot write implementation.

2. As long as a class complies with this agreement, it has all the method declarations in the agreement.

3. As long as the parent class adheres to a protocol, the subclass also follows.

The 4.Protocol declaration method allows any class to be implemented, and Protocol is the Protocol.

5.OC cannot inherit multiple classes (single inheritance) but can comply with multiple protocols. Inheritance (:), Compliance Agreement (< >)

6. The base protocol:<nsobject> is the base protocol and is the most fundamental and basic protocol, which declares many of the most basic methods.

7. The agreement is subject to the agreement, and one agreement complies with the other and can have a method statement in another agreement.

(ii) Basic use

Create a protocol

Compliance Agreement

Implementation of the method declared in the completion protocol

Test program

1. Definition of the Agreement

@protocol Agreement name <NSObject>

Method declaration List

@end;

2. How to comply with the agreement

(1) Class Compliance Agreement

@protocol Class Name: Parent class name < protocol name 1, protocol name 2>

@end

(2) Agreement on compliance with agreements

@protocol Agreement name < other agreement name >

@end;

3. Keywords in the Protocol method declaration

(1) required (default) requires implementation, if not implemented warning but no error

(2) Optional does not require implementation

4. Compliance with protocol restrictions when defining variables

Class name < protocol name > * variable name nsobject<. Myprotocol> *obj;

ID < protocol name > variable name ID <. Myprotocol> obj1;

Attributes declared in 5.Property can also be used to comply with protocol restrictions

@property (Nonatomic, Strong) class name < protocol name > * attribute name;

@property (nonatomic, strong) id< protocol name > attribute name;

6. Supplemental Knowledge: The protocol itself is written in the. h header file, but can also be defined anywhere. When this protocol is only used by this class, the protocol is generally written in this class, and when the protocol requires multiple classes to be implemented, it is written in a separate file.

Objective-c language-description methods and Sel-bolck and protocols

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.