Getting started with iOS development☞OC Language & #183; note 8, iosoc

Source: Internet
Author: User

Getting started with iOS development☞OC language · note 8, iosoc
Block)

1.1 What is block

  • Block is a special data type in iOS. (Variables, input parameters, and return values can be defined)
  • Block is a data type officially recommended by Apple. It is widely used.
    • Animation
    • Multithreading
    • Set Traversal
    • Network request callback

It is also frequently used in multithreading and networks. It is generally used as callback!

  • Role of block
    • Like a function, it is used to save a piece of code and can be retrieved and called at an appropriate time.
    • The block function is similar to a function (method) and more powerful than a function.
    • Block is a data type that can be nested and called at any time.

Xcode can be quickly generated by inputting inline (shortcut key)

1.2 block format

  

  • Comparison functions, function pointers, and blocks:

If the block has no parameters, the () following ^ can be omitted.

  • Block Case

1.3 alias for block type (analogy function pointer)

The variable name is the type name (alias)

Application scenarios of block 1.4

The current side of the code block is the same, the code block is the same, the code block in the middle is different, and the code block in the middle is the same.

    

1.5 block considerations

  • The block can define variables with the same name as the outside world, but the block accesses the variables defined in the block (the proximity principle is the same as the function)

  • Attackers can access external variables in the code block.

(1) You can directly access and modify non-stack zone variables in the code block (the same variable is used)

 

      

(2) variables in the stack can be accessed, but cannot be modified by default (different variables are used, but actually the copied values are accessed)

The variables in the block are not the same as the external local variables. The block copies the external variables to the heap memory.

The external variables used in the block are copied. Therefore, modifying the external variable value before calling does not affect the copy value in the block.

      

(3) Modifying local variables in the code block fails to compile. How can I modify local variables in a code block? Add the keyword __block before the local variable.

  

1.6 interview question: is the block stored in the heap or stack?

  • Block is stored in the stack by default. If a copy operation is performed on the block, the block will be transferred to the heap.
  • If the block is in the stack and the block accesses external objects, the object will not be retained.
  • If the block is in the heap and the block accesses external objects, a retain is performed on the external objects.

    

Memory leakage of 1.7 Blocks

During block copy, objects used inside the block are strongly referenced (ARC) or retainCount is increased by 1 (non-ARC ).

Improper use of blocks in the ARC and non-ARC environments will cause circular references.

Generally:

A class uses block as its own attribute variable, and then the class uses the class itself in the block method body. Simply put

    

1) When MRC is used, loop references can be eliminated using _ block.

2) In the case of ARC, the circular reference problem can be solved only by weak references. After iOS5, you can use _ weak directly. In the past, you can only use _ unsafe_unretained, __unsafe_unretained the disadvantage is that the pointer is not left empty after it is released.

 

When will the Block on the stack be copied to the stack?

  • When the Block copy instance method is called
  • When a Block is returned as a function return value
  • When you assign a Block value to a class with the _ strong modifier, id type, or Block type member variable
  • When a Block is transferred to a Cocoa framework method containing usingBlock in the method name or gcd api

Block is used across two classes.

Implement value transfer between two pages:

  • Use "protocol/proxy" to transfer values between two pages
  • Use block as the property to pass values between two pages

 

Interview Questions:

    

 

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.