黑马程序员---Objective—C第七天课程学习(Foundation)
2015-04-03 00:11
519 查看
Objective-C 第七天课程学习(Foundation)
------Java培训、Android培训、iOS培训、.Net培训、期待与您交流!------
定义一个字符串
NSString *str = @"I love programming in objective-c";
可以通过函数范围机构提返回字符串的位置
如果找不到的话,则length = 0,location = not Found == (int)-1
NSPoint\CGPoint定义一个点
<span style="white-space:pre"> </span>// CGPoint\NSPoint
// 定义一个点
CGPoint point = CGPointMake(8,8);
NSPoint point2 = NSMakePoint(8,9);
CGPoint point3 = NSMakePoint(2,3);
NSPoint point4 = CGPointMake(3.5,4.9);
// 使用这些CGPointEqualToPoint、CGRectContainsPoint等函数的前提是添加CoreGraphics框架
//
// NextStep Foundation
// 比较两个点是否相同(x、y)
BOOL b = CGPointEqualToPoint(CGPointMake(10, 10), CGPointMake(10, 10));
//CGRectEqualToRect(<#CGRect rect1#>, <#CGRect rect2#>)
//CGSizeEqualToSize(<#CGSize size1#>, <#CGSize size2#>)
// x (50, 150) y (40 , 90)
BOOL b2 = CGRectContainsPoint(CGRectMake(50, 40, 100, 50), CGPointMake(60, 45));
NSLog(@"%d", b2);
NSString 不可变字符串
NSMutableString是可变字符串
OC数组不能存放nil。 nil是OC数组元素结束的标志。
有序
快速创建(不可变):@[obj1, obj2, obj3]
快速访问元素:数组名[i]
无序
无序
快速创建(不可变):@{key1 : value1, key2 : value2}
快速访问元素:字典名[key]
* 考察NSString、NSArray的使用
* NSFileManager
*/
#import <Foundation/Foundation.h>
// 计算文件的代码行数
/*
path : 文件的全路径(可能是文件夹、也可能是文件)
返回值 int :代码行数
*/
NSUInteger codeLineCount(NSString *path)
{
// 1.获得文件管理者
NSFileManager *mgr = [NSFileManager defaultManager];
// 2.标记是否为文件夹
BOOL dir = NO; // 标记是否为文件夹
// 标记这个路径是否存在
BOOL exist = [mgr fileExistsAtPath:path isDirectory:&dir];
// 3.如果不存在,直接返回0
if(!exist)
{
NSLog(@"文件路径不存在!!!!!!");
return 0;
}
// 代码能来到着,说明路径存在
if (dir)
{ // 文件夹
// 获得当前文件夹path下面的所有内容(文件夹、文件)
NSArray *array = [mgr contentsOfDirectoryAtPath:path error:nil];
// 定义一个变量保存path中所有文件的总行数
int count = 0;
// 遍历数组中的所有子文件(夹)名
for (NSString *filename in array)
{
// 获得子文件(夹)的全路径
NSString *fullPath = [NSString stringWithFormat:@"%@/%@", path, filename];
// 累加每个子路径的总行数
count += codeLineCount(fullPath);
}
return count;
}
else
{ // 文件
// 判断文件的拓展名(忽略大小写)
NSString *extension = [[path pathExtension] lowercaseString];
if (![extension isEqualToString:@"h"]
&& ![extension isEqualToString:@"m"]
&& ![extension isEqualToString:@"c"])
{
// 文件拓展名不是h,而且也不是m,而且也不是c
return 0;
}
// 加载文件内容
NSString *content = [NSString stringWithContentsOfFile:path encoding:NSUTF8StringEncoding error:nil];
// 将文件内容切割为每一行
NSArray *array = [content componentsSeparatedByString:@"\n"];
// 删掉文件路径前面的/Users/apple/Desktop/iOS课堂共享/0722课堂共享/
NSRange range = [path rangeOfString:@"/Users/apple/Desktop/iOS课堂共享/0722课堂共享/"];
NSString *str = [path stringByReplacingCharactersInRange:range withString:@""];
// 打印文件路径和行数
NSLog(@"%@ - %ld", str, array.count);
return array.count;
}
}
int main()
{
NSUInteger count = codeLineCount(@"/Users/apple/Desktop/iOS课堂共享/0722课堂共享");
NSLog(@"%ld", count);
return 0;
}
void test()
{
NSString *str = @"jack\nrose\njim\njake";
[str writeToFile:@"/Users/apple/Desktop/abc.txt" atomically:YES encoding:NSUTF8StringEncoding error:nil];
NSArray *array = [str componentsSeparatedByString:@"\n"];
for (NSString *line in array)
{
NSLog(@"%@", line);
}
//int count = codeLineCount(@"/Users/apple/Desktop/iOS课堂共享/0722课堂共享/0811/代码/04-block/04-block/main.m");
//NSLog(@"count=%d", count);
}
------Java培训、Android培训、iOS培训、.Net培训、期待与您交流!------
------Java培训、Android培训、iOS培训、.Net培训、期待与您交流!------
1 结构体
在Foundation框架中,常用到的结构体主要有NSRang,NSPoint\CGPoint,NSSize\CGSize,NSRect\CGRect等。1.1 NSRang
NSSRange (location,length)定义一个字符串
NSString *str = @"I love programming in objective-c";
可以通过函数范围机构提返回字符串的位置
如果找不到的话,则length = 0,location = not Found == (int)-1
<span style="font-family:KaiTi_GB2312;"> // 定义义字符串 NSString *str = @"I love programming in Objective-c"; // 查找字符串"ming"的位置 NSRange range = [str rangeOfString:@"ming"]; // 输出位置 NSLog(@"location = %ld,length = %ld",range.location,range.length); NSRange range1 = [str rangeOfString: @"comming"]; NSLog(@"%d",range1.location); // NSLog(@"%ld",range1);</span>
1.2 NSPoint\CGPoint
NSPoint是一个表示点得结构体NSPoint\CGPoint定义一个点
<span style="white-space:pre"> </span>// CGPoint\NSPoint
// 定义一个点
CGPoint point = CGPointMake(8,8);
NSPoint point2 = NSMakePoint(8,9);
CGPoint point3 = NSMakePoint(2,3);
NSPoint point4 = CGPointMake(3.5,4.9);
1.3 NSSize\CGSize
NSSize\CGSize 定义<span style="white-space:pre"> </span>// NSSize\CGSize // 长10,宽20 CGSize size = CGSizeMake(10,20); NSSize size2 = NSMakeSize(10,20); CGSize size3 = NSMakeSize(10,20); NSSize size4 = CGSizeMake(10,20);
1.4 NSRect\CGRect
NSRect\CGRect 结构体表示一个矩形,可以用前面的NSPoint\CGPoint,NSSize\CGSize来定义并初始化<span style="white-space:pre"> </span>// NSRect\CGRect NSRect rect = NSMakeRect(0,0,20,35); CGRect rect2 = CGRectMake(0,0,20,35);
1.5 结构体转换成字符串
// 将结构体转为字符串//NSString *str = NSStringFromPoint(p1); //NSString *str = NSStringFromSize(s3); NSString *str = NSStringFromRect(r1); NSLog(@"%@", str);
1.6 比较两个点是否相同
使用这些CGPointEqualToPoint、CGRectContainsPoint等函数的前提是添加CoreGraphics框架// 使用这些CGPointEqualToPoint、CGRectContainsPoint等函数的前提是添加CoreGraphics框架
//
// NextStep Foundation
// 比较两个点是否相同(x、y)
BOOL b = CGPointEqualToPoint(CGPointMake(10, 10), CGPointMake(10, 10));
//CGRectEqualToRect(<#CGRect rect1#>, <#CGRect rect2#>)
//CGSizeEqualToSize(<#CGSize size1#>, <#CGSize size2#>)
// x (50, 150) y (40 , 90)
BOOL b2 = CGRectContainsPoint(CGRectMake(50, 40, 100, 50), CGPointMake(60, 45));
NSLog(@"%d", b2);
2 字符串
主要学习NSString和NSMutableStringNSString 不可变字符串
NSMutableString是可变字符串
2.1 字符串的创建
/* 1.字符串的创建 */ NSString *s1 = @"jack"; //NSString *s2 = [[NSString alloc] initWithString:@"jack"]; NSString *s3 = [[NSString alloc] initWithFormat:@"age is %d", 10]; // C字符串 --> OC字符串 NSString *s4 = [[NSString alloc] initWithUTF8String:"jack"]; // OC字符串 --> C字符串 const char *cs = [s4 UTF8String]; // NSUTF8StringEncoding 用到中文就可以用这种编码 NSString *s5 = [[NSString alloc] initWithContentsOfFile:@"/Users/apple/Desktop/1.txt" encoding:NSUTF8StringEncoding error:nil]; // URL : 资源路径 // 协议头://路径 // file:// // ftp:// // http://weibo.com/a.png // http://www.baidu.com // NSURL *url = [[NSURL alloc] initWithString:@"file:///Users/apple/Desktop/1.txt"]; NSURL *url = [NSURL fileURLWithPath:@"/Users/apple/Desktop/1.txt"]; NSString *s6 = [[NSString alloc] initWithContentsOfURL:url encoding:NSUTF8StringEncoding error:nil]; NSLog(@"s6=\n%@", s6); /* 一般都会有一个类方法跟对象方法配对 [NSURL URLWithString:<#(NSString *)#>]; [NSString stringWithFormat:@""]; [NSString stringWithContentsOfFile:<#(NSString *)#> encoding:<#(NSStringEncoding)#> error:<#(NSError *__autoreleasing *)#>]; */
2.2 字符串的拼接
NSMutableString *s1 = [NSMutableString stringWithFormat:@"my age is 10"]; // 拼接内容到s1的后面 [s1 appendString:@" 11 12"]; // 获取is的范围 NSRange range = [s1 rangeOfString:@"is"]; [s1 deleteCharactersInRange:range]; NSString *s2 = [NSString stringWithFormat:@"age is 10"]; NSString *s3 = [s2 stringByAppendingString:@" 11 12"]; NSLog(@"s1=%@, s2=%@", s1, s2);
2.3 字符串的导出
// 字符串的导出 [@"Jack\nJack" writeToFile:@"/Users/apple/Desktop/my.txt" atomically:YES encoding:NSUTF8StringEncoding error:nil]; NSString *str = @"4234234"; NSURL *url = [NSURL fileURLWithPath:@"/Users/apple/Desktop/my2.txt"]; [str writeToURL:url atomically:YES encoding:NSUTF8StringEncoding error:nil];
3 集合类
主要学习常见的NSArray\NSMutableArray, NSSet\NSMutableSet,NSDictionary\NSMutableDictionary3.1 NSArray的基本使用
OC数组只能存放OC对象,不能存放非OC对象,如int,enum,struct等。OC数组不能存放nil。 nil是OC数组元素结束的标志。
/* int a = 5; int ages[10] = {1, 90, 89, 17}; Person *p = [[Person alloc] init]; Person *persons[5] = {p, [[Person alloc] init]}; */ // OC数组不能存放nil值 // OC数组只能存放OC对象、不能存放非OC对象类型,比如int、struct、enum等 // 这个array永远是空数组 // NSArray *array = [NSArray array]; /* 1.NSArray的创建 */ NSArray *array2 = [NSArray arrayWithObject:@"jack"]; // nil是数组元素结束的标记 NSArray *array3 = [NSArray arrayWithObjects:@"jack", @"rose", nil]; // [array2 count]; //NSArray *array4 = [NSArray arrayWithObjects:@"jack", @"rose", @"4324324", nil]; // 快速创建一个NSArray对象 NSArray *array4 = @[@"jack", @"rose", @"4324324"]; /* 2.NSArray的元素个数 */ NSLog(@"%ld", array3.count); /* 3.NSArray中元素的访问 */ NSLog(@"%@", [array3 objectAtIndex:1]); //array3[1]; NSLog(@"%@", array3[0]);
3.2 NSMutableArray的基本使用
NSMutableArray *array = [NSMutableArray arrayWithObjects:@"rose", @"jim", nil]; // 添加元素 [array addObject:[[Person alloc] init]]; [array addObject:@"jack"]; // 删除元素 //[array removeAllObjects]; // 删除指定的对象 // [array removeObject:@"jack"]; [array removeObjectAtIndex:0]; // 错误写法 // [array addObject:nil]; NSLog(@"%@", array); NSLog(@"%ld", array.count);
3.3 数组遍历
Person *p = [[Person alloc] init]; NSArray *array = @[p, @"rose", @"jack"]; // for (int i = 0; i<array.count; i++) // { // NSLog(@"%@", array[i]); // } // id obj代表着数组中的每一个元素 //int i = 0; // for (id obj in array) // { // // 找出obj元素在数组中的位置 // NSUInteger i = [array indexOfObject:obj]; // // NSLog(@"%ld - %@", i, obj); // //i++; // // if (i==1) // { // break; // } // } [array enumerateObjectsUsingBlock: // 每遍历到一个元素,就会调用一次block // 并且当前元素和索引位置当做参数传给block ^(id obj, NSUInteger idx, BOOL *stop) { NSLog(@"%ld - %@", idx, obj); if (idx == 0) { // 停止遍历 *stop = YES; } }]; // void ^(myblock)(id, NSUInteger, BOOL *) = ^(id obj, NSUInteger idx, BOOL *stop) // { // NSLog(@"%ld - %@", idx, obj); // // // if (idx == 0) // { // // 停止遍历 // *stop = YES; // } // }; // // for (int i = 0; i<array.count; i++) // { // // 用来标记是否需要停止遍历 // BOOL isStop = NO; // // // 取出元素 // id obj = array[i]; // // myblock(obj, i, &isStop); // // // if (isStop) // { // break; // } // }
3.4 NSSet\ NSMutableSet
// set 基本使用 NSSet *s = [NSSet set]; NSSet *s2 = [NSSet setWithObjects:@"jack",@"rose", @"jack2",@"jack3",nil]; // 随机拿出一个元素 NSString *str = [s2 anyObject]; NSLog(@"%@", str); //NSLog(@"%ld", s2.count);
<p style="margin-top: 0px; margin-bottom: 0px; font-size: 11px; font-family: Menlo; color: rgb(112, 61, 170);">NSMutableSet<span style="font-variant-ligatures: no-common-ligatures; color: #000000"> *s = [</span>NSMutableSet<span style="font-variant-ligatures: no-common-ligatures; color: #000000"> </span><span style="font-variant-ligatures: no-common-ligatures; color: #3d1d81">set</span><span style="font-variant-ligatures: no-common-ligatures; color: #000000">];</span></p><p style="margin-top: 0px; margin-bottom: 0px; font-size: 11px; font-family: Menlo; min-height: 13px;"> </p><p style="margin-top: 0px; margin-bottom: 0px; font-size: 11px; font-family: Menlo; color: rgb(0, 132, 0);"><span style="font-variant-ligatures: no-common-ligatures; color: #000000"> </span>// <span style="font-family: 'Heiti SC Light';">添加元素</span></p><p style="margin-top: 0px; margin-bottom: 0px; font-size: 11px; font-family: Menlo;"> [s <span style="font-variant-ligatures: no-common-ligatures; color: #3d1d81">addObject</span>:<span style="font-variant-ligatures: no-common-ligatures; color: #d12f1b">@"hack"</span>];</p><p style="margin-top: 0px; margin-bottom: 0px; font-size: 11px; font-family: Menlo; min-height: 13px;"> </p><p style="margin-top: 0px; margin-bottom: 0px; font-size: 11px; font-family: Menlo; color: rgb(0, 132, 0);"><span style="font-variant-ligatures: no-common-ligatures; color: #000000"> </span>// <span style="font-family: 'Heiti SC Light';">删除元素</span></p><p style="margin-top: 0px; margin-bottom: 0px; font-size: 11px; font-family: Menlo; color: rgb(0, 132, 0);"><span style="font-variant-ligatures: no-common-ligatures; color: #000000"> </span>// [s removeObject:<#(id)#>];</p>
3.5 NSDictionary 基本使用
{ /* 字典: key ----> value 索引 ----> 文字内容 里面存储的东西都是键值对 */ // NSDictionary *dict = [NSDictionary dictionaryWithObject:@"jack" forKey:@"name"]; // NSArray *keys = @[@"name", @"address"]; // NSArray *objects = @[@"jack", @"北京"]; // NSDictionary *dict = [NSDictionary dictionaryWithObjects:objects forKeys:keys]; /* NSDictionary *dict = [NSDictionary dictionaryWithObjectsAndKeys: @"jack", @"name", @"北京", @"address", @"32423434", @"qq", nil];*/ NSDictionary *dict = @{@"name" : @"jack", @"address" : @"北京"}; // id obj = [dict objectForKey:@"name"]; id obj = dict[@"name"]; NSLog(@"%@", obj); // 返回的是键值对的个数 NSLog(@"%ld", dict.count);
3.6 NSMutableDictionary
NSMutableDictionary *dict = [NSMutableDictionary dictionary]; // 添加键值对 [dict setObject:@"jack" forKey:@"name"]; [dict setObject:@"北京" forKey:@"address"]; [dict setObject:@"rose" forKey:@"name"]; // 移除键值对 // [dict removeObjectForKey:<#(id)#>]; NSString *str = dict[@"name"]; //NSLog(@"%@", str); NSLog(@"%@", dict); //NSLog(@"%@", @[@"jack", @"rose"]);
3.7 集合的总结
3.7.1 NSArray\NSMutableArray
有序
快速创建(不可变):@[obj1, obj2, obj3]
快速访问元素:数组名[i]
3.7.2 NSSet\NSMutableSet
无序
3.7.3 NSDictionary\NSMutableDictionary
无序
快速创建(不可变):@{key1 : value1, key2 : value2}
快速访问元素:字典名[key]
4 NSValue
// NSNumber之所以能包装基本数据类型为对象,是因为继承了NSValue int main() { // 结构体--->OC对象 CGPoint p = CGPointMake(10, 10); // 将结构体转为Value对象 NSValue *value = [NSValue valueWithPoint:p]; // 将value转为对应的结构体 // [value pointValue]; NSArray *array = @[value ]; // insert code here... // NSLog(@"这是哥修改过的东西6"); return 0; }
5 NSDate
int main() { // 09/10/2011 NSString *time = @"2011/09/10 18:56"; NSDateFormatter *formatter = [[NSDateFormatter alloc] init]; formatter.dateFormat = @"yyyy/MM/dd HH:mm"; NSDate *date = [formatter dateFromString:time]; NSLog(@"%@", date); return 0; } void date2string() { NSDate *date = [NSDate date]; // 日期格式化类 NSDateFormatter *formatter = [[NSDateFormatter alloc] init]; // y 年 M 月 d 日 // m 分 s 秒 H (24)时 h(12)时 formatter.dateFormat = @"yyyy-MM-dd HH:mm:ss"; NSString *str = [formatter stringFromDate:date]; NSLog(@"%@", str); } void use() { // 创建一个时间对象 NSDate *date = [NSDate date]; // 打印出的时候是0时区的时间(北京-东8区) NSLog(@"%@", date); NSDate *date2 = [NSDate dateWithTimeInterval:5 sinceDate:date]; // 从1970开始走过的秒数 NSTimeInterval seconds = [date2 timeIntervalSince1970]; // [date2 timeIntervalSinceNow]; }
6 代码示例
/** 考察NSString、NSArray的使用
* NSFileManager
*/
#import <Foundation/Foundation.h>
// 计算文件的代码行数
/*
path : 文件的全路径(可能是文件夹、也可能是文件)
返回值 int :代码行数
*/
NSUInteger codeLineCount(NSString *path)
{
// 1.获得文件管理者
NSFileManager *mgr = [NSFileManager defaultManager];
// 2.标记是否为文件夹
BOOL dir = NO; // 标记是否为文件夹
// 标记这个路径是否存在
BOOL exist = [mgr fileExistsAtPath:path isDirectory:&dir];
// 3.如果不存在,直接返回0
if(!exist)
{
NSLog(@"文件路径不存在!!!!!!");
return 0;
}
// 代码能来到着,说明路径存在
if (dir)
{ // 文件夹
// 获得当前文件夹path下面的所有内容(文件夹、文件)
NSArray *array = [mgr contentsOfDirectoryAtPath:path error:nil];
// 定义一个变量保存path中所有文件的总行数
int count = 0;
// 遍历数组中的所有子文件(夹)名
for (NSString *filename in array)
{
// 获得子文件(夹)的全路径
NSString *fullPath = [NSString stringWithFormat:@"%@/%@", path, filename];
// 累加每个子路径的总行数
count += codeLineCount(fullPath);
}
return count;
}
else
{ // 文件
// 判断文件的拓展名(忽略大小写)
NSString *extension = [[path pathExtension] lowercaseString];
if (![extension isEqualToString:@"h"]
&& ![extension isEqualToString:@"m"]
&& ![extension isEqualToString:@"c"])
{
// 文件拓展名不是h,而且也不是m,而且也不是c
return 0;
}
// 加载文件内容
NSString *content = [NSString stringWithContentsOfFile:path encoding:NSUTF8StringEncoding error:nil];
// 将文件内容切割为每一行
NSArray *array = [content componentsSeparatedByString:@"\n"];
// 删掉文件路径前面的/Users/apple/Desktop/iOS课堂共享/0722课堂共享/
NSRange range = [path rangeOfString:@"/Users/apple/Desktop/iOS课堂共享/0722课堂共享/"];
NSString *str = [path stringByReplacingCharactersInRange:range withString:@""];
// 打印文件路径和行数
NSLog(@"%@ - %ld", str, array.count);
return array.count;
}
}
int main()
{
NSUInteger count = codeLineCount(@"/Users/apple/Desktop/iOS课堂共享/0722课堂共享");
NSLog(@"%ld", count);
return 0;
}
void test()
{
NSString *str = @"jack\nrose\njim\njake";
[str writeToFile:@"/Users/apple/Desktop/abc.txt" atomically:YES encoding:NSUTF8StringEncoding error:nil];
NSArray *array = [str componentsSeparatedByString:@"\n"];
for (NSString *line in array)
{
NSLog(@"%@", line);
}
//int count = codeLineCount(@"/Users/apple/Desktop/iOS课堂共享/0722课堂共享/0811/代码/04-block/04-block/main.m");
//NSLog(@"count=%d", count);
}
------Java培训、Android培训、iOS培训、.Net培训、期待与您交流!------
相关文章推荐
- 黑马程序员——Objective-C 第一天课程学习总结
- 黑马程序员——Objective-C第六天课程学习
- 黑马程序员——Objective-C 第二天课程学习总结
- 黑马程序员——Objective-C 第四天课程学习总结
- 【黑马程序员】iOS学习之路——Objective-C之Foundation
- 黑马程序员——Objective-C 第五天课程学习总结
- 黑马程序员——Objective-C 第三天课程学习总结
- 黑马程序员——Objective-C程序设计(第4版)学习笔记之14-Foundation框架简介——黑马 IOS 技术博客
- 黑马程序员——Objective-C学习笔记(五):foundation Kit (续)
- 【黑马程序员】Objective-C语言学习笔记之小知识点
- 黑马程序员_object c学习日记_复习Foundation
- 黑马程序员_OC学习之Foundation框架1
- 黑马程序员_第七天视频学习 类和对象的三大特性
- 黑马程序员_第六天视频学习 Objective-C的基本语法、类和对象
- 黑马程序员_Objective-C中的ARC学习总结
- 黑马程序员_Foundation框架的学习
- 黑马程序员-我的第十八篇学习日记:Foundation中的NSArray和NAMutableArray
- 黑马程序员-我的第十七篇学习日记:Foundation中的结构体和字符串
- 黑马程序员-OC学习笔记之Foundation框架NSNumber、NSValue和NSDate
- 【黑马程序员】Objective-C语言学习笔记之对象的创建、使用和方法调用(三)