自己总结的
1 //
2 // main.m
3 // 01-结构体
4 //
5 // Created by Mac-ZhangXiaoMeng on 14/12/29.
6 // Copyright (c) 2014年 Mac-ZhangXiaoMeng. All rights reserved.
7 //
8
9 #import <Foundation/Foundation.h>
10
11 int main(int argc, const char * argv[]) {
12 @autoreleasepool {
13
14 // 一、NSRang的基本使用
15 //
16 // NSRange(location位置 length长度) 表示范围
17 //
18 // NSPoint\CGPoint 表示坐标
19 //
20 // NSSize\CGSize 表示UI元素的尺寸
21 //
22 // NSRect\CGRect (CGPint CGSize) 表示一个UI元素的位置和尺寸
23
24
25 /* 1 @"i love oc" // love的范围
26
27 NSRange r1 = {2, 4}; // 不用
28 NSRange r2 = {.location = 2, .length = 4};// 不用
29
30 NSRange r3 = NSMakeRange(2, 4); 掌握
31
32 */
33
34
35 /* 2
36 NSString *str = @"i love oc";
37
38 // 查找某个字符串在str中的范围
39 // 如果找不到,length=0, location=NSNotFound == -1
40 NSRange range = [str rangeOfString:@"love"];
41 NSLog(@"loc = %ld, length=%ld", range.location, range.length);
42 */
43
44
45 // 3. CGPoint p1 = NSMakePoint(10, 10);
46 // NSPoint p2 = CGPointMake(20, 20);// 最常用
47 //
48
49 // 4. NSSize s1 = NSMakeSize(100, 50);
50 // NSSize s2 = CGSizeMake(100, 50);
51 // CGSize s3 = NSMakeSize(200, 60);
52
53 // 5. CGRect r1 = CGRectMake(0, 0, 100, 50);
54
55 // 6. // 将结构体转为字符串
56 //
57 // NSString *str = NSStringFromPoint(p1);
58 //
59 // NSString *str = NSStringFromSize(s3);
60 //
61 // NSString *str = NSStringFromRect(r1);
62 //
63 // NSLog(@"%@", str);
64 // 7. 表示原点
65 // CGPointZero == CGPointMake(0, 0)
66
67 // 8. 比较两个点是否相同(x、y)
68 // BOOL b = CGPointEqualToPoint(CGPointMake(10, 10), CGPointMake(10, 10));
69 //CGRectEqualToRect(<#CGRect rect1#>, <#CGRect rect2#>)
70 //CGSizeEqualToSize(<#CGSize size1#>, <#CGSize size2#>)
71
72 // 9. 右边点是否在矩形块里 x (50, 150) y (40 , 90)
73 // BOOL b2 = CGRectContainsPoint(CGRectMake(50, 40, 100, 50), CGPointMake(60, 45));
74 //
75 // NSLog(@"%d", b2);
76
77
78
79
80 }
81 return 0;
82 }
1 //
2 // main.m
3 // 字符串
4 //
5 // Created by Mac-ZhangXiaoMeng on 14/12/29.
6 // Copyright (c) 2014年 Mac-ZhangXiaoMeng. All rights reserved.
7 //
8
9 #import <Foundation/Foundation.h>
10
11 int main(int argc, const char * argv[]) {
12 @autoreleasepool {
13
14 /*
15 NSString : 不可变字符串
16
17 NSMutableString : 可变字符串
18 */
19
20
21 // 1.字符串的创建
22 // */
23 // NSString *s1 = @"jack";
24 //
25 // NSString *s2 = [[NSString alloc] initWithString:@"jack"];
26 //
27 // NSString *s3 = [[NSString alloc] initWithFormat:@"age is %d", 10];
28 //
29 //
30 // C字符串 --> OC字符串
31 // NSString *s4 = [[NSString alloc] initWithUTF8String:"jack"];
32 // OC字符串 --> C字符串
33 // const char *cs = [s4 UTF8String];
34
35
36
37 // NSUTF8StringEncoding 用到中文就可以用这种编码
38 // NSString *s5 = [[NSString alloc] initWithContentsOfFile:@"/Users/apple/Desktop/1.txt" encoding:NSUTF8StringEncoding error:nil];
39
40
41 // URL : 资源路径
42 // 协议头://路径
43 // file://
44 // ftp://
45 // http://weibo.com/a.png
46
47
48 // http://www.baidu.com
49
50 // NSURL *url = [[NSURL alloc] initWithString:@"file:///Users/apple/Desktop/1.txt"];
51
52 // NSURL *url = [NSURL fileURLWithPath:@"/Users/apple/Desktop/1.txt"];
53 //
54 // NSString *s6 = [[NSString alloc] initWithContentsOfURL:url encoding:NSUTF8StringEncoding error:nil];
55 // NSLog(@"s6=\n%@", s6);
56
57
58
59
60 /*
61 一般都会有一个 类方法 跟对象方法配对
62 [NSURL URLWithString:<#(NSString *)#>];
63
64 [NSString stringWithFormat:@""];
65
66 文件:
67 [NSString stringWithContentsOfFile:<#(NSString *)#> encoding:<#(NSStringEncoding)#> error:<#(NSError *__autoreleasing *)#>];
68
69 */
70
71 //
72 // 3. void stringExport()
73 // {
74 // // 字符串的导出
75 // [@"Jack\nJack" writeToFile:@"/Users/apple/Desktop/my.txt" atomically:YES encoding:NSUTF8StringEncoding error:nil];
76 //
77 //
78 // NSString *str = @"4234234";
79 // NSURL *url = [NSURL fileURLWithPath:@"/Users/apple/Desktop/my2.txt"];
80 // [str writeToURL:url atomically:YES encoding:NSUTF8StringEncoding error:nil];
81 // }
82
83
84
85
86 // 4.
87 //
88 NSMutableString *s1 = [NSMutableString stringWithFormat:@"my age is 10"];
89 // 拼接内容到s1的后面
90 [s1 appendString:@"11 12"];
91
92 // 获取is的范围
93 NSRange range = [s1 rangeOfString:@"is"];
94 [s1 deleteCharactersInRange:range]; //删掉is
95
96
97 NSString *s2 = [NSString stringWithFormat:@"age is 10"];
98
99 //又创建一个字符串
100 NSString *s3 = [s2 stringByAppendingString:@" 11 12"];
101
102
103 NSLog(@"s1=%@\n, s2=%@\n,s3=%@\n", s1, s2,s3);
104 //
105
106
107
108
109 // //快速视频
110
111
112 //#pragma mark 字符串的大小写处理
113 // void caseTest() {
114 // NSString *str = @"GuangDong";
115 // // 转成大写
116 // NSLog(@"大写:%@", [str uppercaseString]);
117 // // 转成小写
118 // NSLog(@"小写:%@", [str lowercaseString]);
119 // // 首字母变大写,其他字母变小写
120 // NSLog(@"首字母变大写:%@", [@"aGE" capitalizedString]);
121 // }
122 //
123 //#pragma mark 字符串的比较
124 // void compare() {
125 // // 检测字符串的内容是否相同
126 // BOOL result = [@"abc" isEqualToString:@"abc"];
127 // NSLog(@"%i", result);
128 //
129 // // NSOrderedAscending 右边的字符串比左边大
130 // // NSOrderedSame 两个字符串的内容相同
131 // // NSOrderedDescending 左边的字符串比右边的大
132 // NSComparisonResult result2 = [@"abc" compare:@"Abc"];
133 // if (result2 == NSOrderedSame) {
134 // NSLog(@"两个字符串的内容相同");
135 // } else if (result2 == NSOrderedAscending) {
136 // NSLog(@"右边 > 左边");
137 // } else if (result2 == NSOrderedDescending) {
138 // NSLog(@"右边 < 左边");
139 // }
140 // }
141 //
142 //#pragma mark 字符串的搜索
143 // void search() {
144 // NSString *str = @"123456456.txt";
145 //
146 // NSLog(@"是否以22开头:%i", [str hasPrefix:@"22"]);
147 // NSLog(@"是否以txt结尾:%i", [str hasSuffix:@"txt"]);
148 //
149 // // 搜索字符串
150 // NSRange range = [str rangeOfString:@"456"];
151 // // range.length == 0
152 // if (range.location == NSNotFound) {
153 // NSLog(@"不能找到");
154 // } else {
155 // NSLog(@"找到的范围是:%@", NSStringFromRange(range));
156 // }
157 //
158 // // 从尾部开始搜索字符串
159 // range = [str rangeOfString:@"456" options:NSBackwardsSearch];
160 // NSLog(@"%@", NSStringFromRange(range));
161 //
162 // // 指定范围进行搜索
163 // // [str rangeOfString:@"456" options:NSBackwardsSearch range:<#(NSRange)#>];
164 // }
165 //
166 //#pragma mark 字符串的截取
167 // void subString() {
168 // NSString *str = @"123456";
169 //
170 // // 从索引3开始截取到尾部(包括3)
171 // NSLog(@"%@", [str substringFromIndex:3]);
172 //
173 // // 从头部开始截取到索引3之前(不包括3)
174 // NSLog(@"%@", [str substringToIndex:3]);
175 //
176 // // 指定范围进行截取
177 // NSRange range = NSMakeRange(2, 3);
178 // NSLog(@"%@", [str substringWithRange:range]);
179 //
180 // NSString *str2 = @"a-b-c-d-5";
181 // NSArray *array = [str2 componentsSeparatedByString:@"-"];
182 // NSLog(@"%@", array);
183 //
184 // NSString *str3 = [array objectAtIndex:0];
185 // NSLog(@"%@", str3);
186 // }
187 //
188 //#pragma mark 与路径相关
189 // void pathTest() {
190 // // 快速创建一个自动释放的数组
191 // NSMutableArray *components = [NSMutableArray array];
192 // [components addObject:@"Users"];
193 // [components addObject:@"MJ"];
194 // [components addObject:@"Desktop"];
195 // // 将数组中的所有字符串拼接成一个路径
196 // NSString *path = [NSString pathWithComponents:components];
197 // NSLog(@"%@", path);
198 //
199 // // 将路径分解成一个数组
200 // NSArray *cmps = [path pathComponents];
201 // NSLog(@"%@", cmps);
202 //
203 // // path是一个字符串常量,是不可变的
204 // path = @"/users/mj/test";
205 // // 判断是够为绝对路径(依据是前面有无/)
206 // NSLog(@"%i", [path isAbsolutePath]);
207 // NSLog(@"最后一个目录:%@", [path lastPathComponent]);
208 // // 删除最后一个目录
209 // NSLog(@"%@", [path stringByDeletingLastPathComponent]);
210 // // 在最后面拼接一个目录
211 // NSLog(@"%@", [path stringByAppendingPathComponent:@"abc"]);
212 // }
213 //
214 //#pragma mark 拓展名处理
215 // void extension() {
216 // NSString *str = @"/User/MJ/test.txt";
217 //
218 // NSLog(@"拓展名:%@", [str pathExtension]);
219 // // 删除拓展名
220 // NSLog(@"%@", [str stringByDeletingPathExtension]);
221 // // 添加拓展名
222 // NSLog(@"%@", [@"abc" stringByAppendingPathExtension:@"mp3"]);
223 // }
224 //
225 //#pragma mark 其他用法
226 // void other() {
227 // NSString *str = @"12";
228 // int a = [str intValue];
229 // NSLog(@"%i", a);
230 //
231 // // 计算字数,不是计算字符数
232 // NSLog(@"length=%zi", [@"我是字符串123" length]);
233 //
234 // // 取出对应的字符
235 // unichar c = [@"abc" characterAtIndex:0];
236 // NSLog(@"%c", c);
237 //
238 // // 返回C语言中的字符串
239 // const char *s = [@"abc" UTF8String];
240 // NSLog(@"%s", s);
241 // }
242 //
243
244
245 //#pragma mark 可变字符串的创建
246 // void stringCreate() {
247 // // 预先分配10个字数的存储空间
248 // NSMutableString *str = [[NSMutableString alloc] initWithCapacity:10];
249 // // 设置字符串内容
250 // [str setString:@"1234"];
251 //
252 // // 拼接一个字符串
253 // [str appendString:@"567"];
254 // // 拼接字符串
255 // [str appendFormat:@"age is %i and height is %.2f", 27, 1.55f];
256 //
257 // // 替换字符串
258 // NSRange range = [str rangeOfString:@"height"];
259 // //NSRange range = NSMakeRange(7, 3);
260 // [str replaceCharactersInRange:range withString:@"no"];
261 //
262 // // 插入字符串
263 // [str insertString:@"abc" atIndex:2];
264 //
265 // // 删除字符串
266 // range = [str rangeOfString:@"age"];
267 // [str deleteCharactersInRange:range];
268 //
269 // NSLog(@"%@", str);
270 //
271 // // 释放对象
272 // [str release];
273 // }
274
275
276
277 }
278 return 0;
279 }
1 //
2 // main.m
3 // NSArray
4 //
5 // Created by Mac-ZhangXiaoMeng on 14/12/30.
6 // Copyright (c) 2014年 Mac-ZhangXiaoMeng. All rights reserved.
7 //
8
9 #import <Foundation/Foundation.h>
10 #import "Person.h"
11 #import "Student.h"
12 int main(int argc, const char * argv[]) {
13 @autoreleasepool {
14
15 /*
16
17 NSArray :不可变数组
18 NSMutableArray : 可变数组
19
20 */
21
22
23 //NSArray *array = @[@"jack", @"rose"];
24
25
26
27
28 /* 1.
29 int a = 5;
30
31 int ages[10] = {1, 90, 89, 17};
32
33 Person *p = [[Person alloc] init];
34 Person *persons[5] = {p, [[Person alloc] init]};
35 */
36
37 // OC数组不能存放nil值
38 // OC数组只能存放OC对象、不能存放非OC对象类型,比如int、struct、enum等
39
40 // 这个array永远是空数组
41 // NSArray *array = [NSArray array];
42
43
44 /*
45 2.NSArray的创建
46
47 NSArray *array2 = [NSArray arrayWithObject:@"jack"];
48
49
50 // nil是数组元素结束的标记,有2个元素不可变
51 NSArray *array3 = [NSArray arrayWithObjects:@"jack", @"rose", nil];
52
53 //NSArray *array4 = [NSArray arrayWithObjects:@"jack", @"rose", @"4324324", nil];
54
55
56 // 快速创建一个NSArray对象
57 NSArray *array4 = @[@"jack", @"rose", @"4324324"];
58
59 */
60
61 /*
62
63 2.NSArray的元素个数
64
65 NSLog(@"%ld", array3.count);
66
67
68
69 3.NSArray中元素的访问
70
71 NSLog(@"%@", [array3 objectAtIndex:1]);
72
73 //array3[1];
74 NSLog(@"%@", array3[0]);
75
76
77 */
78
79
80 // 4.遍历数组
81
82 // Person *p = [[Person alloc] init];
83 //
84 //
85 // NSArray *array = @[p, @"rose", @"jack"];
86 // // 第一种
87 // for (int i = 0; i<array.count; i++)
88 // {
89 // NSLog(@"%i-%@", i,array[i]);
90 // }
91
92 //第二种
93 //快速遍历
94 // id obj代表着数组中的每一个元素
95 // int i = 0;
96 // for (id obj in array)
97 // {
98 // // 找出obj元素在数组中的位置
99 // //NSUInteger i = [array indexOfObject:obj];
100 //
101 // NSLog(@"%ld - %@", i, obj);
102 // i++;
103 //
104 // if (i==3)
105 // {
106 // break;
107 // }
108 // }
109 //
110
111 // 第三种
112 // 每遍历到一个元素,就会调用一次block
113 // 并且当前元素和索引位置当做参数传给block
114
115 // [array enumerateObjectsUsingBlock:
116 //
117 // ^(id obj, NSUInteger idx, BOOL *stop)
118 // {
119 // NSLog(@"%ld - %@", idx, obj);
120 //
121 //
122 // if (idx == 1)
123 // {
124 // // 停止遍历
125 // *stop = YES;
126 // }
127 //
128 // }];
129
130
131 //6. 可变数组的基本使用
132 //
133 // NSMutableArray *array = [NSMutableArray arrayWithObjects:@"rose", @"jim", nil];
134 //
135 // // 添加元素
136 // //[array addObject:[[Person alloc] init]];
137 //
138 // [array addObject:@"jack"];
139 //
140 // // 删除所有元素
141 // //[array removeAllObjects];
142 // // 删除指定的对象
143 // // [array removeObject:@"jack"];
144 // //[array removeObjectAtIndex:0];
145 //
146 //
147 // // [array addObject:nil];// 错误写法
148 //
149 //
150 // NSLog(@"%@", array);
151 //
152 // NSLog(@"%ld", array.count);
153 //
154 //
155 //
156 // // @[] 只创建不可变数组NSArray
157 // /* 错误写法
158 // NSMutableArray *array = @[@"jack", @"rose"];
159 //
160 // [array addObject:@"jim"];
161 // */
162
163
164
165 //4. 快速视频
166
167
168 //#pragma mark 创建一个数组
169 // void arrayCreate() {
170 // // 创建一个空的数组
171 // NSArray *array = [NSArray array];
172 //
173 // // 创建有1个元素的数组
174 // array = [NSArray arrayWithObject:@"123"];
175 //
176 // // 创建有多个元素的数组
177 // array = [NSArray arrayWithObjects:@"a", @"b", @"c", nil];
178 //
179 // int count = [array count];
180 // // count = array.count;
181 // NSLog(@"%i", count);
182 // }
183 //
184 //#pragma mark 数组的简单使用
185 // void arrayUse() {
186 // NSObject *obj = [[NSObject alloc] init];
187 // NSArray *array = [NSArray arrayWithObjects:@"a", @"b", @"c" , obj, nil];
188 // // 判断是否包含了某个元素
189 // if ([array containsObject:@"a"]) {
190 // NSLog(@"包含了字符串a");
191 // }
192 //
193 // NSString *last = [array lastObject];
194 // NSLog(@"last=%@", last);
195 //
196 // NSString *str = [array objectAtIndex:1];
197 // NSLog(@"%@", str);
198 //
199 // int index = [array indexOfObject:@"c"];
200 // NSLog(@"index=%i", index);
201 //
202 // [obj release];
203 // }
204 //
205 //#pragma mark 数组的内存管理
206 // void arrayMemory() {
207 // // 1
208 // Student *stu1 = [[Student alloc] init];
209 // Student *stu2 = [[Student alloc] init];
210 // Student *stu3 = [[Student alloc] init];
211 //
212 // NSLog(@"stu1:%zi", [stu1 retainCount]);
213 //
214 // // 当把一个对象塞进数组中时,这个对象的计数器会加1,也就是说数组会对它做一次retain操作
215 // // 2
216 // NSArray *array = [[NSArray alloc] initWithObjects:stu1, stu2, stu3, nil];
217 //
218 // NSLog(@"stu1:%zi", [stu1 retainCount]);
219 //
220 // NSLog(@"count=%zi", array.count);
221 //
222 // // 1
223 // [stu1 release];
224 // [stu2 release];
225 // [stu3 release];
226 //
227 // // 数组被销毁的时候,会对内部的所有元素都做一次release操作
228 // // 0
229 // [array release];
230 // }
231 //
232 //#pragma mark 给数组里面的元素发送消息
233 // void arrayMessage() {
234 // Student *stu1 = [Student student];
235 // Student *stu2 = [Student student];
236 // Student *stu3 = [Student student];
237 //
238 // NSArray *array = [NSArray arrayWithObjects:stu1, stu2, stu3, nil];
239 // // 让数组里面的所有对象都调用test方法
240 // // [array makeObjectsPerformSelector:@selector(test)];
241 // [array makeObjectsPerformSelector:@selector(test2:) withObject:@"123"];
242 // }
243 //
244 //#pragma mark 遍历数组1
245 // void arrayFor1() {
246 // Student *stu1 = [Student student];
247 // NSArray *array = [NSArray arrayWithObjects:stu1, @"1", @"2", @"3", nil];
248 // int count = array.count;
249 // for (int i = 0; i<count; i++) {
250 // // id == void *
251 // id obj = [array objectAtIndex:i];
252 // NSLog(@"%i-%@", i, obj);
253 // }
254 // }
255 //
256 //#pragma mark 遍历数组2
257 // void arrayFor2() {
258 // Student *stu1 = [Student student];
259 // NSArray *array = [NSArray arrayWithObjects:stu1, @"1", @"2", @"3", nil];
260 // // 快速遍历
261 // int i =0;
262 // for (id obj in array) {
263 // NSLog(@"%i-%@", i, obj);
264 // i++;
265 // }
266 // }
267 //
268 //#pragma mark 遍历数组3
269 // void arrayFor3() {
270 // Student *stu1 = [Student student];
271 // NSArray *array = [NSArray arrayWithObjects:stu1, @"1", @"2", @"3", nil];
272 // [array enumerateObjectsUsingBlock:
273 // ^(id obj, NSUInteger idx, BOOL *stop) {
274 // NSLog(@"%i-%@", idx, obj);
275 //
276 // // 如果索引为1,就停止遍历
277 // if (idx == 1) {
278 // // 利用指针修改外面BOOL变量的值
279 // *stop = YES;
280 // }
281 // }];
282 // }
283 //
284 //#pragma mark 遍历数组4
285 // void arrayFor4() {
286 // Student *stu1 = [Student student];
287 // NSArray *array = [NSArray arrayWithObjects:stu1, @"1", @"2", @"3", nil];
288 //
289 // // 获取数组的迭代器
290 // // NSEnumerator *enumerator = [array objectEnumerator];
291 // // 反序迭代器(从尾部开始遍历元素)
292 // NSEnumerator *enumerator = [array reverseObjectEnumerator];
293 //
294 // // allObjects是取出没有被遍历过的对象
295 // NSArray *array2 = [enumerator allObjects];
296 // NSLog(@"array2:%@", array2);
297 //
298 // // 获取下一个需要遍历的元素
299 // id obj = nil;
300 // while (obj = [enumerator nextObject]) {
301 // NSLog(@"obj=%@", obj);
302 // }
303 // }
304 //
305 //
306
307 //
308 //#pragma mark 派生出新的数组
309 // void arrayNew() {
310 // NSArray *array = [NSArray arrayWithObjects:@"1", @"2", nil];
311 //
312 // NSArray *array2 = [array arrayByAddingObject:@"3"];
313 //
314 // NSArray *array3 = [array arrayByAddingObjectsFromArray:[NSArray arrayWithObjects:@"4", @"5", nil]];
315 //
316 // NSLog(@"array:%@", array);
317 // NSLog(@"array2:%@", array2);
318 // NSLog(@"array3:%@", array3);
319 //
320 //
321 // NSArray *array4 = [NSArray arrayWithObjects:@"1", @"2", @"3", @"4", nil];
322 // NSRange range = NSMakeRange(1, 2);
323 // NSArray *array5 = [array4 subarrayWithRange:range];
324 // NSLog(@"array5:%@", array5);
325 // }
326 //
327 //#pragma mark 数组的其他用法
328 // void arrayOther() {
329 // NSArray *array = [NSArray arrayWithObjects:@"1", @"2", @"3", @"4", nil];
330 // // 1-2-3-4
331 // // 利用分隔符-拼接所有的数组元素
332 // NSString *str = [array componentsJoinedByString:@"-"];
333 // NSLog(@"%@", str);
334 //
335 // // 将一个数组写入文件(生成的是一个xml文件)
336 // NSString *path = @"/Users/apple/Desktop/array.xml";
337 // [array writeToFile:path atomically:YES];
338 //
339 //
340 // path = @"/Users/apple/Desktop/array.txt";
341 // // 从文件中读取数组内容(文件有严格的格式要求)
342 // NSArray *array2 = [NSArray arrayWithContentsOfFile:path];
343 // NSLog(@"array2:%@", array2);
344 // }
345 //
346 //#pragma mark 数组排序1
347 // void arraySort1() {
348 // NSArray *array = [NSArray arrayWithObjects:@"2", @"3", @"1", @"4", nil];
349 //
350 // // 返回一个排好序的数组,原来数组的元素顺序不会改变
351 // // 指定元素的比较方法:compare:
352 // NSArray *array2 = [array sortedArrayUsingSelector:@selector(compare:)];
353 // NSLog(@"array2:%@", array2);
354 // }
355 //
356 //#pragma mark 数组排序2
357 // void arraySort2() {
358 // Student *stu1 = [Student studentWithFirstname:@"MingJie" lastname:@"Li"];
359 // Student *stu2 = [Student studentWithFirstname:@"LongHu" lastname:@"Huang"];
360 // Student *stu3 = [Student studentWithFirstname:@"LianJie" lastname:@"Li"];
361 // Student *stu4 = [Student studentWithFirstname:@"Jian" lastname:@"Xiao"];
362 // NSArray *array = [NSArray arrayWithObjects:stu1,stu2,stu3, stu4, nil];
363 //
364 // // 指定排序的比较方法
365 // NSArray *array2 = [array sortedArrayUsingSelector:@selector(compareStudent:)];
366 //
367 // NSLog(@"array2:%@", array2);
368 // }
369 //
370 //#pragma mark 数组排序3
371 //
372 // Student *stu1 = [Student studentWithFirstname:@"MingJie" lastname:@"Li"];
373 // Student *stu2 = [Student studentWithFirstname:@"LongHu" lastname:@"Huang"];
374 // Student *stu3 = [Student studentWithFirstname:@"LianJie" lastname:@"Li"];
375 // Student *stu4 = [Student studentWithFirstname:@"Jian" lastname:@"Xiao"];
376 // NSArray *array = [NSArray arrayWithObjects:stu1,stu2,stu3, stu4, nil];
377 //
378 // // 利用block进行排序
379 // NSArray *array2 = [array sortedArrayUsingComparator:
380 // ^NSComparisonResult(Student *obj1, Student *obj2) {
381 // // 先按照姓排序
382 // NSComparisonResult result = [obj1.lastname compare:obj2.lastname];
383 // // 如果有相同的姓,就比较名字
384 // if (result == NSOrderedSame) {
385 // result = [obj1.firstname compare:obj2.firstname];
386 // }
387 //
388 // return result;
389 // }];
390 //
391 // NSLog(@"array2:%@", array2);
392
393
394 //#pragma mark 数组排序4-高级排序
395 // void arraySort4() {
396 // Student *stu1 = [Student studentWithFirstname:@"MingJie" lastname:@"Li" bookName:@"book1"];
397 // Student *stu2 = [Student studentWithFirstname:@"LongHu" lastname:@"Huang" bookName:@"book2"];
398 // Student *stu3 = [Student studentWithFirstname:@"LianJie" lastname:@"Li" bookName:@"book2"];
399 // Student *stu4 = [Student studentWithFirstname:@"Jian" lastname:@"Xiao" bookName:@"book1"];
400 // NSArray *array = [NSArray arrayWithObjects:stu1,stu2,stu3, stu4, nil];
401 //
402 // // 1.先按照书名进行排序
403 // // 这里的key写的是@property的名称
404 // NSSortDescriptor *bookNameDesc = [NSSortDescriptor sortDescriptorWithKey:@"book.name" ascending:YES];
405 // // 2.再按照姓进行排序
406 // NSSortDescriptor *lastnameDesc = [NSSortDescriptor sortDescriptorWithKey:@"lastname" ascending:YES];
407 // // 3.再按照名进行排序
408 // NSSortDescriptor *firstnameDesc = [NSSortDescriptor sortDescriptorWithKey:@"firstname" ascending:YES];
409 // // 按顺序添加排序描述器
410 // NSArray *descs = [NSArray arrayWithObjects:bookNameDesc, lastnameDesc, firstnameDesc, nil];
411 //
412 // NSArray *array2 = [array sortedArrayUsingDescriptors:descs];
413 //
414 // NSLog(@"array2:%@", array2);
415 // }
416
417
418
419 }
420 return 0;
421 }
1 //
2 // main.m
3 // NSSet
4 //
5 // Created by Mac-ZhangXiaoMeng on 14/12/30.
6 // Copyright (c) 2014年 Mac-ZhangXiaoMeng. All rights reserved.
7 //
8
9 #import <Foundation/Foundation.h>
10
11 int main(int argc, const char * argv[]) {
12 @autoreleasepool {
13
14
15 /*
16 NSSet和NSArray的对比
17 1> 共同点
18 * 都是集合,都能存放多个OC对象
19 * 只能存放OC对象,不能存放非OC对象类型(基本数据类型:int、char、float等,结构体,枚举)
20 * 本身都不可变,都有一个可变的子类
21
22 2> 不同点
23 * NSArray有顺序,NSSet没有顺序
24 */
25
26
27
28 // 1.
29 // NSSet *s = [NSSet set];
30 //
31 // NSSet *s2 = [NSSet setWithObjects:@"jack",@"rose", @"jack2",@"jack3",nil];
32 //
33 // // 随机拿出一个元素
34 // NSString *str = [s2 anyObject];
35 //
36 // NSLog(@"%@", str);
37 //
38 // //NSLog(@"%ld", s2.count);
39
40 // 2.
41 // NSMutableSet *s = [NSMutableSet set];
42 //
43 // // 添加元素
44 // [s addObject:@"hack"];
45 // [s addObject:@"jack"];
46 //
47 //
48 // // 删除某个元素
49 // [s removeObject:@"jack"];
50 // // 删除所有元素
51 // // [s removeAllObjects ];
52 // NSLog(@"%@",s);
53
54
55
56
57
58
59
60
61
62
63
64
65
66 }
67 return 0;
68 }
1 //
2 // main.m
3 // 04-计算代码行数
4 //
5 // Created by apple on 13-8-12.
6 // Copyright (c) 2013年 itcast. All rights reserved.
7 //
8
9 /*
10 * 考察NSString、NSArray的使用
11 * NSFileManager
12 */
13
14 #import <Foundation/Foundation.h>
15
16
17 // 计算文件的代码行数
18 /*
19 path : 文件的全路径(可能是文件夹、也可能是文件)
20 返回值 int :代码行数
21 */
22 NSUInteger codeLineCount(NSString *path)
23 {
24 // 1.获得文件管理者
25 NSFileManager *mgr = [NSFileManager defaultManager];
26
27 // 2.标记是否为文件夹
28 BOOL dir = NO; // 标记是否为文件夹
29 // 标记这个路径是否存在
30 BOOL exist = [mgr fileExistsAtPath:path isDirectory:&dir];
31
32 // 3.如果不存在,直接返回0
33 if(!exist)
34 {
35 NSLog(@"文件路径不存在!!!!!!");
36 return 0;
37 }
38
39 // 代码能来到着,说明路径存在
40
41
42 if (dir)
43 { // 文件夹
44 // 获得当前文件夹path下面的所有内容(文件夹、文件)
45 NSArray *array = [mgr contentsOfDirectoryAtPath:path error:nil];
46
47 // 定义一个变量保存path中所有文件的总行数
48 int count = 0;
49
50 // 遍历数组中的所有子文件(夹)名
51 for (NSString *filename in array)
52 {
53 // 获得子文件(夹)的全路径
54 NSString *fullPath = [NSString stringWithFormat:@"%@/%@", path, filename];
55
56 // 累加每个子路径的总行数
57 count += codeLineCount(fullPath);
58 }
59
60 return count;
61 }
62 else
63 { // 文件
64 // 1.判断文件的拓展名(忽略大小写)
65 NSString *extension = [[path pathExtension] lowercaseString];
66 if (![extension isEqualToString:@"h"]
67 && ![extension isEqualToString:@"m"]
68 && ![extension isEqualToString:@"c"])
69 {
70 // 文件拓展名不是h,而且也不是m,而且也不是c
71 return 0;
72 }
73
74 // 2.加载文件内容
75 NSString *content = [NSString stringWithContentsOfFile:path encoding:NSUTF8StringEncoding error:nil];
76
77 //3.将文件内容切割为每一行
78 NSArray *array = [content componentsSeparatedByString:@"\n"];
79 NSLog(@"%@ - %ld", path, array.count);
80 return array.count;
81
82
83 // //4. 删掉文件路径前面的/Users/Mac/desktop/oc
84 // NSRange range = [path rangeOfString:@"/Users/Mac/desktop/oc/"];
85 // NSString *str = [path stringByReplacingCharactersInRange:range withString:@""];
86 //
87 // // 打印文件路径和行数
88 // NSLog(@"%@ - %ld", str, array.count);
89 //
90 }
91 }
92
93
94
95
96 int main()
97 {
98
99 NSUInteger count = codeLineCount(@"/Users/Mac/desktop/oc");
100
101 NSLog(@"%ld", count);
102
103
104 return 0;
105 }
106
107
108
109
110
111 void test()
112 {
113 //练习 文本的代码行数
114 NSString *str = @"jack\nrose\njim\njake";
115
116 [str writeToFile:@"//Users/Mac/desktop/练习" atomically:YES encoding:NSUTF8StringEncoding error:nil];
117
118
119 NSArray *array = [str componentsSeparatedByString:@"\n"];
120
121 for (NSString *line in array)
122 {
123 NSLog(@"%@", line);
124 }
125
126
127 //int count = codeLineCount(@"/Users/apple/Desktop/iOS课堂共享/0722课堂共享/0811/代码/04-block/04-block/main.m");
128
129 //NSLog(@"count=%d", count);
130 }
1 //
2 // main.m
3 // NSDictionary
4 //
5 // Created by Mac-ZhangXiaoMeng on 14/12/30.
6 // Copyright (c) 2014年 Mac-ZhangXiaoMeng. All rights reserved.
7 //
8
9 #import <Foundation/Foundation.h>
10
11 int main(int argc, const char * argv[]) {
12 @autoreleasepool {
13
14 /*
15 集合
16 1.NSArray\NSMutableArray
17 * 有序
18 * 快速创建(不可变):@[obj1, obj2, obj3]
19 * 快速访问元素:数组名[i]
20
21 2.NSSet\NSMutableSet
22 * 无序
23
24 3.NSDictionary\NSMutableDictionary
25 * 无序
26 * 快速创建(不可变):@{key1 : value1, key2 : value2}
27 * 快速访问元素:字典名[key]
28 */
29
30
31 /*
32 字典:
33
34 key ----> value
35 索引 ----> 文字内容
36
37 里面存储的东西都是键 值对
38
39 */
40 // 2.
41
42 // NSDictionary *dict = [NSDictionary dictionaryWithObject:@"jack" forKey:@"name"];
43
44 // 3.
45 // NSArray *keys = @[@"name", @"address"];
46 // NSArray *objects = @[@"jack", @"北京"];
47
48 // NSDictionary *dict = [NSDictionary dictionaryWithObjects:objects forKeys:keys];
49 // id obj = [dict objectForKey:@"name"];
50 // NSLog(@"%@",obj); //打印名字
51
52
53 /* 4.
54 NSDictionary *dict = [NSDictionary dictionaryWithObjectsAndKeys:
55 @"jack", @"name",
56 @"北京", @"address",
57 @"32423434", @"qq", nil];
58
59 */
60
61 // 5.简化的 记住
62 // NSDictionary *dict = @{@"name" : @"jack", @"address" : @"北京"};
63 // id obj = dict[@"name"];//等价 id obj = [dict objectForKey:@"name"];
64 // NSLog(@"%@", obj);
65
66 // // 6. 返回的是键值对的个数
67 // NSLog(@"%ld", dict.count);
68
69
70
71 // NSMutableDictionary
72
73
74 // NSMutableDictionary *dict = [NSMutableDictionary dictionary];
75 //
76 // // 添加键值对
77 // [dict setObject:@"jack" forKey:@"name"];
78 //
79 //
80 // [dict setObject:@"北京" forKey:@"address"];
81 // //覆盖以前的name
82 // [dict setObject:@"rose" forKey:@"name"];
83 //
84 //
85 // // 移除键值对
86 // //[dict removeObjectForKey:@"name"];
87 //
88 //
89 // NSString *str = dict[@"name"];
90 //
91 //
92 // NSLog(@"%@", str);
93 //
94 // NSLog(@"%@", dict);
95
96
97 //NSLog(@"%@", @[@"jack", @"rose"]);
98
99
100
101 // 错误做法右边为不可变字典
102 // NSMutableDictionary *dict = @{@"name" : @"jack"};
103 //
104 // [dict setObject:@"rose" forKey:@"name"];
105
106
107 // 7. 遍历字典
108
109 //字典不允许有相同的key,但允许有相同的value(Object)
110 // 字典的无序的
111 //
112 // NSDictionary *dict = @{
113 // @"address" : @"北京",
114 // @"name" : @"jack",
115 // @"name2" : @"jack",
116 // @"name3" : @"jack",
117 // @"qq" : @"7657567765"};
118 ////
119 //
120 // // 第一种
121 // NSArray *keys = [dict allKeys];
122 //
123 // for (int i = 0; i<dict.count; i++)
124 // {
125 // NSString *key = keys[i];
126 // NSString *object = dict[key];
127 //
128 // NSLog(@"%@ = %@", key, object);
129 // }
130 //
131 //
132 // // 第二种block
133 // [dict enumerateKeysAndObjectsUsingBlock:
134 // ^(id key, id obj, BOOL *stop) {
135 // NSLog(@"%@ - %@", key, obj);
136 //
137 // // *stop = YES;
138 // }];
139
140
141
142 // 8.
143
144
145 // NSArray *persons = @[
146 // @{@"name" : @"jack", @"qq" : @"432423423", @"books": @[@"5分钟突破iOS编程", @"5分钟突破android编程"]},
147 // @{@"name" : @"rose", @"qq" : @"767567"},
148 // @{@"name" : @"jim", @"qq" : @"423423"},
149 // @{@"name" : @"jake", @"qq" : @"123123213"}
150 // ];
151 //
152 // //
153 // // NSDictionary *jim = persons[2];
154 //
155 //
156 // //
157 // NSString *bookName = persons[0][@"books"][1];
158 // NSLog(@"%@", bookName);
159 // //NSArray *array = persons[0][@"books"];
160 //
161 // //NSLog(@"%@", array);
162 //
163 // // 先取出1位置对应的字典
164 // // 再取出字典中qq这个key对应的数据
165 // //NSLog(@"%@", persons[1][@"qq"]);
166 //
167 // // NSLog(@"%@", jim);
168 //
169
170
171 // 快速视频
172
173
174 //#pragma mark 字典的初始化
175 // void dictCreate() {
176 // // NSDictionary是不可变的
177 // NSDictionary *dict = [NSDictionary dictionaryWithObject:@"v" forKey:@"k"];
178 //
179 // // 最常用的初始化方式
180 // dict = [NSDictionary dictionaryWithObjectsAndKeys:
181 // @"v1", @"k1",
182 // @"v2", @"k2",
183 // @"v3", @"k3", nil];
184 //
185 // NSArray *objects = [NSArray arrayWithObjects:@"v1", @"v2", @"v3", nil];
186 // NSArray *keys = [NSArray arrayWithObjects:@"k1", @"k2", @"k3", nil];
187 // dict = [NSDictionary dictionaryWithObjects:objects forKeys:keys];
188 // NSLog(@"%@", dict);
189 // }
190 //
191 //#pragma mark 字典的基本用法
192 // void dictUse() {
193 // NSDictionary *dict = [NSDictionary dictionaryWithObjectsAndKeys:
194 // @"v1", @"k1",
195 // @"v2", @"k2",
196 // @"v3", @"k3", nil];
197 //
198 // // count是计算有多少个键值对(key-value)
199 // NSLog(@"count=%zi", dict.count);
200 //
201 // // 由于NSDictionary是不可变的,所以只能取值,而不能修改值
202 // id obj = [dict objectForKey:@"k2"];
203 // NSLog(@"obj=%@", obj);
204 //
205 // // 将字典写入文件中
206 // NSString *path = @"/Users/apple/Desktop/dict.xml";
207 // [dict writeToFile:path atomically:YES];
208 //
209 // // 从文件中读取内容
210 // dict = [NSDictionary dictionaryWithContentsOfFile:path];
211 // NSLog(@"dict=%@", dict);
212 // }
213 //
214 //#pragma mark 字典的用法
215 // void dictUse2() {
216 // NSDictionary *dict = [NSDictionary dictionaryWithObjectsAndKeys:
217 // @"v1", @"k1",
218 // @"v2", @"k2",
219 // @"v3", @"k3", nil];
220 // // 返回所有的key
221 // NSArray *keys = [dict allKeys];
222 // //NSLog(@"keys=%@", keys);
223 //
224 // NSArray *objects = [dict allValues];
225 // //NSLog(@"objects=%@", objects);
226 //
227 // // 根据多个key取出对应的多个value
228 // // 当key找不到对应的value时,用marker参数值代替
229 // objects = [dict objectsForKeys:[NSArray arrayWithObjects:@"k1", @"k2", @"k4", nil] notFoundMarker:@"not-found"];
230 // NSLog(@"objects=%@", objects);
231 // }
232 //
233 //#pragma mark 遍历字典
234 // void dictFor() {
235 // NSDictionary *dict = [NSDictionary dictionaryWithObjectsAndKeys:
236 // @"v1", @"k1",
237 // @"v2", @"k2",
238 // @"v3", @"k3", nil];
239 // // 遍历字典的所有key
240 // for (id key in dict) {
241 // id value = [dict objectForKey:key];
242 // NSLog(@"%@=%@", key, value);
243 // }
244 // }
245 //
246 //#pragma mark 遍历字典2
247 // void dictFor2() {
248 // NSDictionary *dict = [NSDictionary dictionaryWithObjectsAndKeys:
249 // @"v1", @"k1",
250 // @"v2", @"k2",
251 // @"v3", @"k3", nil];
252 // // key迭代器
253 // NSEnumerator *enumer = [dict keyEnumerator];
254 // id key = nil;
255 // while ( key = [enumer nextObject]) {
256 // id value = [dict objectForKey:key];
257 // NSLog(@"%@=%@", key, value);
258 // }
259 //
260 // // 对象迭代器
261 // // [dict objectEnumerator];
262 // }
263 //
264 //#pragma mark 遍历字典3
265 // void dictFor3() {
266 // NSDictionary *dict = [NSDictionary dictionaryWithObjectsAndKeys:
267 // @"v1", @"k1",
268 // @"v2", @"k2",
269 // @"v3", @"k3", nil];
270 // [dict enumerateKeysAndObjectsUsingBlock:
271 // ^(id key, id obj, BOOL *stop) {
272 // NSLog(@"%@=%@", key, obj);
273 // }];
274 // }
275 //
276 //#pragma mark
277 // void dictMemory() {
278 // Student *stu1 = [Student studentWithName:@"stu1"];
279 // Student *stu2 = [Student studentWithName:@"stu2"];
280 // Student *stu3 = [Student studentWithName:@"stu3"];
281 //
282 // // 一个对象称为字典的key或者value时,会做一次retain操作,也就是计数器会+1
283 // NSDictionary *dict = [NSDictionary dictionaryWithObjectsAndKeys:
284 // stu1, @"k1",
285 // stu2, @"k2",
286 // stu3, @"k3", nil];
287 //
288 // // 当字典被销毁时,里面的所有key和value都会做一次release操作,也就是计数器会-1
289 // }
290
291
292 //#pragma mark 可变字典的使用
293 // void dictUse() {
294 // // 创建一个空的字典
295 // NSMutableDictionary *dict = [NSMutableDictionary dictionary];
296 // Student *stu1 = [Student studentWithName:@"stu1"];
297 // Student *stu2= [Student studentWithName:@"stu2"];
298 //
299 // // 添加元素
300 // // stu1的计数器会+1
301 // [dict setObject:stu1 forKey:@"k1"];
302 // NSLog(@"stu1:%zi", [stu1 retainCount]);
303 //
304 // // 添加其他字典other到当前字典dict中
305 // NSDictionary *other = [NSDictionary dictionaryWithObject:@"v2" forKey:@"k2"];
306 // [dict addEntriesFromDictionary:other];
307 //
308 // // 删除所有的键值对
309 // // [dict removeAllObjects];
310 //
311 // // 删除k1对应的元素stu1,stu1会做一次release操作
312 // [dict removeObjectForKey:@"k1"];
313 // NSLog(@"stu1:%zi", [stu1 retainCount]);
314 //
315 // // 删除多个key对应的value
316 // // [dict removeObjectsForKeys:[NSArray arrayWithObject:@"k1"]];
317 //
318 // // 字典被销毁时,内部的所有key和value计数器都会-1,也就是说stu1会release一次
319 // }
320 //
321
322
323 }
324 return 0;
325 }
1 //
2 // main.m
3 // NSNumber
4 //
5 // Created by Mac-ZhangXiaoMeng on 14/12/30.
6 // Copyright (c) 2014年 Mac-ZhangXiaoMeng. All rights reserved.
7 //
8
9 #import <Foundation/Foundation.h>
10
11 int main(int argc, const char * argv[]) {
12 @autoreleasepool {
13
14
15
16 // void test ()
17 // {
18 // NSNumber *num = [NSNumber numberWithInt:10];
19 //
20 // NSDictionary *dict = @{
21 // @"name" : @"jack",
22 //
23 //
24 // @"age" : num
25 //
26 // };
27 //
28 // NSNumber *num2 = dict[@"age"];
29 //
30 //
31 // int a = [num2 intValue];
32 //
33 // NSLog(@"%d" , a);
34 // }
35 //
36
37 // 2.
38 // @20 将 20包装成一个NSNumber对像
39
40
41 // NSArray *array = @[
42 //
43 // @{@"name" : @"jack", @"age" : @20},
44 //
45 // @{@"name" : @"rose", @"age" : @25},
46 //
47 // @{@"name" : @"jim", @"age" : @27}
48 // ];
49 //
50 //
51 // // 将各种基本数据类型包装成NSNumber对象
52 // @10.5;
53 // @YES;
54 // @'A'; // NSNumber对象
55 //
56 // @"A"; // NSString对象
57 //
58 //
59 //
60 // // 将age变量包装成NSNumber对象
61 // int age = 100;
62 // @(age);
63 // //[NSNumber numberWithInt:age];
64 //
65 //
66 // NSNumber *n = [NSNumber numberWithDouble:10.5];
67 // int d = [n doubleValue];
68 //
69 //
70 //
71 // int a = 20;
72 // // @"20"
73 // NSString *str = [NSString stringWithFormat:@"%d", a];
74 // NSLog(@"%d", [str intValue]);
75
76 }
77 return 0;
78 }
1 //
2 // main.m
3 // NSValue
4 //
5 // Created by Mac-ZhangXiaoMeng on 14/12/30.
6 // Copyright (c) 2014年 Mac-ZhangXiaoMeng. All rights reserved.
7 //
8
9 #import <Foundation/Foundation.h>
10
11 int main(int argc, const char * argv[]) {
12 @autoreleasepool {
13
14 // // NSNumber之所以能包装基本数据类型为对象,是因为继承了NSValue
15 //
16 // // 结构体--->OC对象
17 // //结构体
18 // CGPoint p = CGPointMake(10, 10);
19 // // 将结构体转为Value对象
20 // NSValue *value = [NSValue valueWithPoint:p];
21 //
22 // // 将value转为对应的结构体
23 // // [value pointValue];
24 //
25 // NSArray *array = @[value ];
26 //
27
28
29 }
30 return 0;
31 }
1 //
2 // main.m
3 // NSDate
4 //
5 // Created by Mac-ZhangXiaoMeng on 14/12/30.
6 // Copyright (c) 2014年 Mac-ZhangXiaoMeng. All rights reserved.
7 //
8
9 #import <Foundation/Foundation.h>
10
11 int main(int argc, const char * argv[]) {
12 @autoreleasepool {
13
14
15 // void use()
16 // {
17 // // 创建一个时间对象
18 // NSDate *date = [NSDate date];
19 // // 打印出的时候是0时区的时间(北京-东8区)
20 // NSLog(@"%@", date);
21 //
22 //
23 // 比date晚5秒钟
24 // NSDate *date2 = [NSDate dateWithTimeInterval:5 sinceDate:date];
25
26 // // 从1970开始走过的秒数
27 // NSTimeInterval seconds = [date2 timeIntervalSince1970];
28 //
29 // // [date2 timeIntervalSinceNow];
30 // }
31 //
32
33
34 // 2.记住
35 NSDate *date = [NSDate date];
36 // 日期格式化类
37 NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
38
39 // y 年 M 月 d 日
40 // m 分 s 秒 H (24)时 h(12)时
41 formatter.dateFormat = @"yyyy-MM-dd HH:mm:ss";
42
43 NSString *str = [formatter stringFromDate:date];
44
45 NSLog(@"%@", str);
46
47
48 // // 09/10/2011
49 // NSString *time = @"2011/09/10 18:56";
50 //
51 // NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
52 // formatter.dateFormat = @"yyyy/MM/dd HH:mm";
53 //
54 // NSDate *date = [formatter dateFromString:time];
55 // NSLog(@"%@", date);
56 //
57 //
58
59
60 }
61 return 0;
62 }