iOS development--oc Base-arc, BLOCK, protocol

Source: Internet
Author: User

First, ARC

ARC is a compiler feature! Rather than the iOS runtime features, and the garbage collection mechanism in Java is completely different
ARC is a new feature that has been added since iOS 5, completely eliminating the cumbersome manual management of memory, and the compiler will automatically insert the appropriate retain, release, and autorelease statements where appropriate. You no longer need to worry about memory management, because the compiler handles everything for you
ARC rule: As long as there is a strong pointer variable pointing to the object, the object remains in memory
The weak pointer automatically becomes a nil pointer when the object pointed by the weak pointer is reclaimed, and no wild pointer error is thrown
Use Note:
1, not allowed to call release, retain, Retaincount;
2. Allow overriding of the Dello method, but the override Dello method does not allow [super delloc];
3. @property Parameters:
Strong: The member variable created by the delegate is a strong pointer, similar to the original retain, (for OC objects)
Weak: the member variable created by the delegate is a pointer, similar to the original Assgin, (for OC objects)
Assign: This applies to non-OC objects, such as int, long, and so on
4, the former retain to change to strong
5, if there is a circular reference usage, then one end with strong end with weak
For example: A dog has a master, a master has a dog!

    1. <p>
    2. @interface Dog:nsobject
    3. @property (nonatomic,weak) person *person; The dog has a Master object, this pointer is a weak pointer,
    4. @end </p><p> @interface Person:nsobject
    5. @property (Nonatomic,strong) Dog *dog; Man owns a dog object, this pointer is a strong pointer,
    6. @end </p><p> @implementation Person
    7. @end </p><p> @impleme </p>
Copy Code


Second, block

Blocks are like functions, which is one way to save your code:
1, can save the code;
2, there is a return value;
3, tangible parameters;
4, call the same way

    1. <p><p>//block with no formal parameter, if no formal parameter can omit the following parentheses
    2. void (^myblock) () = ^{
    3. NSLog (@ "my Block");
    4. };</p><p>//block with formal parameters
    5. Block with two parameters for two int type data and
    6. Int (^heblock) (int,int) = ^ (int a, int b) {
    7. return a + B;
    8. };</p><p>//output n horizontal lines with a block
    9. void (^shuchu) (int) = ^ (int n) {
    10. for (int i= 0;i<n;i++)
    11. NSLog (@ "———————");
    12. };</p>

Defining block types with typedef

    1. <p><p>typedef Int (^myblock) (int, int); </p><p>myblcok he = ^ (int a, int b) {
    2. return a + B;
    3. };</p><p>int C = He (19,9); Assign values of 19 and 9 to c;</p>

Summarize:
1> How to define Block
Int (^heblock) (int,int);
void (^myblock) ();
2> How to encapsulate code with block
^{nslog (@ "my Block");
3>block access to variables outside
External variables can be accessed inside the *block
* Block cannot modify the outside local variables by default
* Add _block keyword to local variable, this local variable can be modified inside block
such as: int a = 11;
_block int b = 12;
The above two variables A and B only B can be modified in the block!

4. Define block variables with typedef
typedef int (^myblock) (int, int);
After defining a block variable, you can then use my block to define the block variable directly
Such as:

    1. <p>
    2. Myblock A, B;
    3. Myblock c;</p><p>b = ^ (int a, int b) {
    4. return a * b;
    5. };//Note the semicolon behind this curly brace cannot! Because this is like an assignment operation! </p><p>myblock d = ^ (int a, int b) {
    6. return a * b;
    7. };</p>


Third, the agreement
Protocol @protocol used to declare a bunch of methods (cannot declare member variables)
A protocol is used to declare a method, so long as a class adheres to the protocol, it has a declaration of the method in this protocol!
Note: As long as the parent class adheres to a protocol, the subclass also has a protocol
: Inherit parent class
<> Compliance Agreements
Parameters of the protocol:

    1. <p>
    2. @required//must be implemented, not implemented will be error </p><p> @optional//not required to be implemented, not implemented without warning </p><p> @protocol Myportocol <nsobject>//all protocols are subject to the base protocol.
    3. -(void) test1;</p><p> @required
    4. -(void) test2;
    5. -(void) test3;</p><p> @optional
    6. -(void) test4;
    7. -(void) test5;</p><p> @end
    8. @required @optional keywords are mostly used for communication between programmers,
    9. And it's for proxy mode.
    10. A class can obey multiple classes </p><p> @interface class names: Parent class < Protocol 1, Protocol 2>//This class has all the methods of both Protocols </p>
    1. Base protocol: an agreement that any protocol complies with NSObject
    2. The base protocol is a parent class in the definition of a similar class, which is simply a protocol that can be followed by a protocol.
    3. If an agreement complies with an agreement, then the agreement has all the method statements that comply with the agreement.
    4. @protocol MyProtocol1 <MyProtocol>
    5. -(void) test;
    6. @end


In the example above, MyProtocol1 has all the method declarations in the MyProtocol protocol.
Restricting object types:

    1. <p>
    2. Restricting a pointer variable can only save objects that obey some kind of protocol;</p><p>dog<myprotocol> *dog; This dog only holds objects that inherit the dog class and comply with the MyProtocol protocol </p><p>
    3. @interface dog:nsobject <MyProtocol></p><p> @property (nonatomic, strong) id<myprotocol2> mm; Create an object member variable that can only Pao objects of the MYPROTOCOL2 protocol
    4. @end </p>



Advance declaration of the agreement

    1. <p><p> @protocol XXXXX; Declare xxxx is a protocol, but if you want to use the method of the declaration within this Protocol, then you have to #import "XXXXX.H" </p><p>//later in the. H file will be used in the @class/@protocol to declare a class/ Agreement!
    2. Because this is more efficient, it is only #import when it comes to the specific declaration of the specific content/protocol of this class "/< ></p>



Summary: Remember the following usage firmly
1, the definition of the agreement
@protocol Agreement name <NSObject>
Declaration of the method
@end
2. How to comply with the agreement
1> Class Compliance Agreement
@interface Class Name: Parent class name < protocol 1, Protocol 2>
@end
2> Protocol Compliance Agreement
@protocol Agreement name < Protocol 1, Protocol 2>
@end
3. Declaration of methods in the Protocol keyword
1> @required (default)
Requires implementation, and warns if no implementation is made
2> @optional
Not required to be implemented, not implemented without warning
4. When defining a variable, limit the object to which the variable is stored to comply with a protocol
Class name < protocol name > * variable name;
Dog<myprotocol> *dog;
id< protocol name > variable name;
Id<myprotocol> Dog;
If the corresponding protocol is not followed, the compiler will report a warning
5. Attributes declared in the @property can also be limited by a compliance with a protocol
@property <nomotomic,strong> dog<myprotocol> *dog;
@property <nomotomic,strong> ID <MyProtocol> dog;
Attention!! If the compiler's warning adds a horizontal line to a code, this warning is a critical warning that must be fixed
6, the protocol can be defined in other files can also be set in a class
The declaration of a class can be written in the same file when only one class is required to comply with it;
When a protocol requires more than one class to follow, the protocol is written in a single file
7, classification can also be defined in a separate. h and. m files, can also be defined in the original class
Attention!! In general, they are defined in a separate file! Definition in the original class is only required to understand the line!
Because if it is defined in the original class of the file, why build classification Le! Is it not?

Proxy design pattern (Application of protocol)
When defining a class that has a member variable (which is an object), it is necessary to restrict the object to a specific protocol, so I don't have to move when I write this class. I only accept the member variables that abide by this protocol, this way I implement it in accordance with the Protocol I need to directly invoke the method of the line, do not care about this member variable will become who!
For example:
Create an agreement with two ways to check the fare and the number of tickets
Dailixieyi.h file

    1. <p>
    2. #import <Foundation/Foundation.h>
    3. @protocol dailixieyi<nsobject>
    4. -(int) piaoshu;</p><p>-(int) Piaojia;
    5. @end </p>

    1. <p>//Daili1.h File
    2. #import <Foundation/Foundation.h>
    3. #import "Dailixieyi.h" </p><p> @interface daili1:nsobject <Dailixieyi></p><p> @end < /P

    1. <p>//daili1.m File
    2. #import <Foundation/Foundation.h>
    3. #import "Daili1.h" </p><p> @implementation Daili1
    4. -(int) Piaoshu
    5. {
    6. Return 15;//Assuming back there are 15 tickets
    7. }</p><p>-(int) Piaojia
    8. {
    9. Return 150;//assumes that the fare returned is $50
    10. }
    11. @end </p>



————————————————————————————————————————————————————————————

    1. <p>//guke.h file
    2. #import <Foundation/Foundation.h>
    3. #import "Dailixieyi.h"
    4. Attention! You only have this agreement here, but when you want to use a class, you have to #import the declaration of this class!
    5. This design pattern only cares if I need a class when the declaration of this class is available.
    6. @interface guke:nsobject</p><p> @property (nonatomic,strong) id<dailixieyi> daili;// ID Type no matter what object I can receive but must conform to dailixieyi this protocol!!
    7. -(void) Maipiao;
    8. @end </p>

iOS development--oc Base-arc, BLOCK, protocol

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.