Objective-c block method and laughter grammar

Source: Internet
Author: User

#pragma mark-block Syntax

NSLog(@ "========block method 1===========");

[Students sortusingcomparator: ^nscomparisonresult(ID obj1, ID obj2) {

Student *stu1 = (Student *) obj1; the obj1 here is the Student type.

Student *stu2 = (Student *) obj2;

NSLog (@ "%ld", [STU1. Name COMPARE:STU2. Name]);

return [STU1. Name COMPARE:STU2. Name];

}];

NSLog(@ "======= method 2==========");

nscomparator sortbyname = ^Nscomparisonresult (Student *obj1,Student *obj2)

{

return [Obj1. Name compare:obj2. Name];

};

[Students Sortusingcomparator:sortbyname];

NSLog (@ "%@", students);

// age

NSLog(@ "=========age=========");

[Students sortusingcomparator:^nscomparisonresult (Student *obj1, Student *obj2) {

if (obj1. Age > Obj2. Age ) {

return nsordereddescending;

}Else if (obj1. Age < Obj2. Age )

{

return nsorderedascending;

}

return nsorderedsame;

}];

NSLog (@ "%@", students);

// study No.

NSLog(@ "==========num=========");

[Students sortusingcomparator:^nscomparisonresult (Student *obj1, Student *obj2) {

if (obj1. num > Obj2. num) {

return nsordereddescending;

}Else if (obj1. Num < Obj2. num)

{

return nsorderedascending;

}

return nsorderedsame;

}];

NSLog (@ "%@", students);

#pragma mark- lol grammar

laugh Grammar Get string

NSString *str = [[NSString alloc] initwithutf8string:" convert C language to OC string " ];

nsstring *str1 = @ " laughing and laughing Grammar ";

laugh Grammar Get array

Nsarray *arr = [[nsarray alloc] initwithobjects:str,str1, Nil];

Nsarray *arr1 = @[str,str1];

[Arr Objectatindex:0];

ARR[0];

NSLog (@ "%@", arr[1]);

Laugh Grammar get dictionary

NSLog(@ "======= get Dictionary ========");

Nsdictionary *dic = [[Nsdictionary alloc] initwithobjectsandkeys:str,@ "str", str1,@ "str1", nil];

nsdictionary *dic1 = @{@ "str": str,@ "str1": str1};

NSLog (@ "%@", Dic1);

Laugh grammar get value by key

[Dic1 Valueforkey:@ "str1"];

NSLog (@ "%@", dic1[@ "str1"]);

Laugh grammar Get value object

NSNumber *NUM10 = [NSNumber numberwithint:ten];

@10;

@1.2;

NSLog (@ "%@", NUM10);

//Laughter grammar created , are immutable .

}

return 0;

}

Objective-c block method and laughter grammar

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.