OC Learning Day Sixth (some errors encountered while knocking the code)

Source: Internet
Author: User

1. How do I define a block?

The left side of the equals sign is the declaration to the right.

Declaration part: First write the return value type then (Block) then (parameter name); Be sure to enclose the parentheses.

Implementation section: ^ toeic character return value type

2.how block is applied : array sorting

Issues to be aware of:

1 "Object" is used when comparing.

Like what:

Student *STU1 = [[Student alloc]initwithname:@ "Wangbadan" withsex:@ "nan" withage:13 withscore:77 Withnumber:3];

Nsarray *array = [Nsarray arraywithobjects:stu1,stu2,stu3, Nil];

Note that this is used when stu1 (object) instead of @ "STU1"

2 when a non-changing comparison is made, there is a return value type of

Array = [array sortedarrayusingcomparator:^nscomparisonresult (ID obj1, id obj2)

If written in this way is wrong:

[Array sortedarrayusingcomparator:^nscomparisonresult (ID obj1, id obj2)

Because the return value type of sortedarrayusingcomparator is an array, you must use an array to receive it. no sorting is done from the results of the output

3 Think (for in) loop output

4 What to do when you do the project:

Block is implemented using the C-language structure (it can be researched when doing the project)

Nsinteger __block count = 0; (add two bars in front of block)

Nsinteger count = 0;

Nsinteger (^stringblock) (NSString *) = ^nsinteger (NSString *string) {

Nsinteger number = [string IntegerValue];

Count++;//block the internal copy process, a local variable is copied over as a constant to use (this notation is changed block, if not added Nsinteger __block count = 0, to achieve, if not add is wrong. )

CountNumber = countNumber-5;

Global variables can be used inside the block or modified

Number =number +count+countnumber;

return number;

};

Nsinteger result= Stringblock (@ "3");

NSLog (@ "%lu", result);

If you want to change the local variables of the block, write the __block on it.

OC Learning Day Sixth (some errors encountered while knocking the code)

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.