标签:
1 #import <Foundation/Foundation.h>
2
3
4 #import "Student.h"
5
6 int c =200;
7 int main(int argc, const char * argv[]) {
8 @autoreleasepool {
9 /*
10 -------------------------------------
11 定义block变量的四种方法
12 -------------------------------------
13 */
14 //无返回值,无形参
15 void (^myblock)() = ^(){
16 NSLog(@"-------");
17 };
18 myblock();
19
20 //无返回值有参数的block
21 void (^lineBlock)(int) = ^(int n)
22 {
23 for (int i=0; i<n; i++) {
24 NSLog(@"---------");
25 }
26 };
27 lineBlock(5);
28 //有返回值无形参
29 int (^myblock3)() = ^(){
30 int a=2;
31 int b=3;
32 return a+b;
33 };
34 int a =myblock3();
35 NSLog(@"%d",a);
36
37 //有返回值,有形参
38 int (^sumblock)(int,int)=^(int a,int b)
39 {
40 return a+b;
41 };
42 int c = sumblock(10,11);
43 NSLog(@"%d",c);
44
45
46
47
48 /*
49 ---------------------------------------
50
51 Block的访问局部变量和全局变量的问题
52
53 ----------------------------------------
54
55 */
56
57
58
59 int a1=8; //全局变量
60 void (^n)(int)=^(int a){
61 // a=7; //局部变量(重名时优先使用)
62 NSLog(@"---===%d",a1);
63 NSLog(@"%p",&a1);
64 };
65 n(a);
66 NSLog(@"----%d",a1);
67 NSLog(@"%p",&a1);
68
69 //block内部可以访问外部局部变量的值,如果想要修改要用__block
70 __block int a2=8;//使用__block可以修改局部变量
71 NSLog(@"------%p",&a2);
72 void (^myblock1)()=
73 ^{
74 a2=7;
75 NSLog(@"---%d",a2);
76 NSLog(@"-%p",&a2);
77 };
78 myblock1(a);
79 NSLog(@"--%d",a2);
80 NSLog(@"------%p",&a2);
81
82 //block内部 可以使用 外界的局部变量
83 __block int num = 1;
84 void (^addNum)() = ^()
85 {
86 //在block内部实现面部分,访问不到局部变量
87 //但是当使用__block后就可以修改局部变量
88 num++;
89 int c = num*10;
90 NSLog(@"%d",c);
91 };
92
93 //调用block
94 addNum();
95 NSLog(@"count = %d",num);
96
97
98 //block 与全局变量
99 //在block内部可以访问全局变量,并且可以修改全局变量
100
101 /*
102 //int c = 200;定义在main上面
103
104 void (^add)() = ^()
105 {
106 c++;
107 NSLog(@"c = %d",c);
108 };
109 add ();
110 NSLog(@"---%d",c);
111
112
113 */
114
115
116 /*
117 -------------------------------------------
118 Block与数组的配合使用(多用于排序)
119 --------------------------------------------
120 */
121
122
123
124
125 //利用block与对数组进行排序(此种方式默认为升序)
126
127 NSArray *array = [NSArray arrayWithObjects:@"as",@"er",@"hfg", nil];
128 NSComparator sortBlock = ^(id string1, id string2){
129
130 return [string1 compare:string2];
131
132 };
133 NSArray *sortArray = [array sortedArrayUsingComparator:sortBlock];
134 NSLog(@"%@",sortArray);
135
136
137 /*
138 students是?一个NSMutableArray对象,包含若干个Student对象。按
139 student的age进?行排序。对姓名进行排序.(student需要自己定义)
140
141 */
142 Student *stu1 = [[Student alloc]initWithName:@"aiaohua" number:@"b1" age:12];
143 Student *stu2 = [[Student alloc]initWithName:@"xiaoming" number:@"b2" age:15];
144 Student *stu3 = [[Student alloc]initWithName:@"giaowang" number:@"b3" age:14];
145 Student *stu4 = [[Student alloc]initWithName:@"oiaofang" number:@"b4" age:11];
146
147 NSMutableArray *num1 = [NSMutableArray arrayWithObjects:stu1,stu2,stu3,stu4, nil];
148
149 [num1 sortUsingComparator:
150 ^NSComparisonResult(id a, id b)
151 {
152 Student *st1 = (Student*)a;
153 Student *st2 = (Student*)b;
154 if ([st1 age]>[st2 age]) {
155 return NSOrderedDescending;
156 }
157 else if([st1 age]<[st2 age]){
158 return NSOrderedAscending;
159 }else {
160 return NSOrderedSame;
161 }
162
163 }];
164 //用description方法重写后得出结果
165 NSLog(@"%@",num1);
166
167 //按照姓名顺序打印
168 [num1 sortUsingComparator:^NSComparisonResult(id a,id b)
169 {
170 Student *a1 = (Student*)a;
171 Student *b1 = (Student*)b;
172 if ([[a1 name] compare:[b1 name]]>0) {
173 return NSOrderedDescending;
174 } else if ([[a1 name] compare:[b1 name]]<0)
175 {
176 return NSOrderedAscending;
177 }else{
178 return NSOrderedSame;
179 }
180
181 }];
182
183 for (int i=0; i<[num1 count]; i++) {
184 NSLog(@"%@",[num1[i] name]);
185 }
186
187 /*
188
189 ------------------------------------------
190 ios的字面量
191 ------------------------------------------
192
193 */
194
195 //一般方法输出数组,字符串,字典,集合
196
197
198 //数组
199 NSArray *arr = [[NSArray alloc]initWithObjects:@"123",@"456",@"678", nil];
200 NSLog(@"%@",arr);
201 NSArray *arr1 = [NSArray arrayWithObjects:@"21",@"43",@"65", nil];
202 NSLog(@"%@",arr1);
203 //字符串
204 NSString *str = [NSString stringWithFormat:@"%s %d%c","sdf",34,‘d‘];
205 NSLog(@"%@",str);
206 NSString *str1 = [NSString stringWithUTF8String:"huige"];
207 NSLog(@"%@",str1);
208 NSString *str2 = [NSString stringWithString:str1];
209 NSLog(@"%@",str2);
210 //字典
211 NSDictionary *dic = [NSDictionary dictionaryWithObjectsAndKeys:@"one",@"1",@"two",@"2",@"three",@"3", nil];
212 NSLog(@"%@",dic);
213 NSDictionary *dic1 = [NSDictionary dictionaryWithDictionary:dic];
214 NSLog(@"%@",dic1);
215 //集合
216 NSSet *set = [NSSet setWithObjects:@"hui",@"deng",@"xiao", nil];
217 NSLog(@"%@",set);
218 NSSet *set1 = [[NSSet alloc]initWithObjects:@"one",@"two",@"three", nil];
219 NSLog(@"%@",set1);
220
221
222 //用ios字面量定义数组,字符串,字典
223 //定义字符串
224 NSString *str3 = @"hollow";
225 NSLog(@"%@",str3);
226 //定义数组
227 NSArray *arr3 = @[@"12",@"53",@"67"];
228 NSLog(@"%@",arr3);
229 //输出指定元素
230 NSLog(@"--%@",arr3[2]);
231 //定义字典
232 NSDictionary *dic3 = @{@"1":@"one",@"2":@"two",@"3":@"three"};
233 NSLog(@"%@",dic3);
第六讲 Block块语法及Block与数组的排序,ios字面量的使用(源代码上传)
标签:
原文地址:http://www.cnblogs.com/erdeng/p/4768036.html