Small knowledge, big challenge! This article is participating in the creation activity of “Essential Tips for Programmers”.

I. Classification (Category)

1. Easy to use

  • The statement formats@interface Name of the class (category name) @end
  • Realize the format@implementation Class name (category name) @end
  • Multiple categories can be added to a class

2. Pay attention to detail

  • Categories cannot add attributes
  • True private properties of this class cannot be accessed directly, only through getters and setters

Informal agreements

  • Add categories for classes provided by the system

Third, extend

  • Extension is a special classification.

    • Isn’t there a name
    • The added method is declared, not implemented, and shares the implementation of the method with the class
  • Syntax: @interface This class name () @end

  • usage

    • When defining a private member for a class, just define it in the implementation of the class
  • Classes can write @property, only generate getter and setter declarations, and in extension, generate more private properties

Second,blockThe data type

An overview,

1. It is a data type
  • Specifically used to store a piece of code

  • Declare the syntactic format of block variables

    Return value type (^block variable name)(argument list);Copy the code
  • Format the code snippet

    ^ Return value type (argument list){// code snippet};Copy the code
  • For example

  • void(^myBlock)()=^void(){ NSLog(@"good"); }

2. Use
  • Syntax format

    Block variable name ();Copy the code
3. The abbreviations
  • There is no return value and void can be omitted

  • Without arguments, you can omit the parentheses

  • Both of the above are true and omitted

  • If the code snippet already specifies parameters, you can just write the type of the parameter

    int(^myBlock3)(int,int)=^int(int num1,int num2){
        int num3=num1+num2;
        return num3;
    };
    Copy the code
4.typedefTo simplify theblockThe definition of
  • Syntax format

    Typedef Return value type (^ new type)(argument list);Copy the code

Second,blockInternal access to external variables

  • inblcokInside the code snippet, you can get the values of external variables, both local and global
  • Inside a code block, only external global variables can be modified
Three,blockAs arguments to the function
  • Write a block variable of the specified format inside the parentheses of the declaration argument

    void text(void(^myblock)()){
        
    }
    Copy the code
  • Use a typedef simplified block as an argument

    void text(NewType block1){
        
    }
    Copy the code

Four,blockAs the return value of the function

  • The type of the return value must be specified bytypedefReductively defined

5. Differences with Block

  • Same: both are wrapped code
  • Different:
    • A block is a data type
    • Block variables can be declared
    • Blick can be used as an argument to a function

Original blog, welcome advice!