收集Block隘擎、Protocol殴穴、結(jié)構(gòu)體、NSString、NSArray采幌、NSFileManager劲够、NSSet、NSDictionary休傍、NSNumber征绎、NSDate、NSValue等的基本用法尊残。
Block
block要掌握的東西
1> 如何定義block變量
int (^sumBlock)(int, int);
void (^myBlock)();
2> 如何利用block封裝代碼
^(int a, int b) {
return a - b;
};
^() {
NSLog(@"----------");
};
^ {
NSLog(@"----------");
};
3> block訪問外面變量
* block內(nèi)部可以訪問外面的變量
* 默認情況下炒瘸,block內(nèi)部不能修改外面的局部變量
* 給局部變量加上__block關(guān)鍵字,這個局部變量就可以在block內(nèi)部修改
4> 利用typedef定義block類型
typedef int (^MyBlock)(int, int);
// 以后就可以利用MyBlock這種類型來定義block變量
MyBlock block;
MyBlock b1, b2;
b1 = ^(int a, int b) {
return a - b;
};
MyBlock b3 = ^(int a, int b) {
return a - b;
};
Protocol
1.協(xié)議的定義
@protocol 協(xié)議名稱 <NSObject>
// 方法聲明列表....
@end
2.如何遵守協(xié)議
1> 類遵守協(xié)議
@interface 類名 : 父類名 <協(xié)議名稱1, 協(xié)議名稱2>
@end
2> 協(xié)議遵守協(xié)議
@protocol 協(xié)議名稱 <其他協(xié)議名稱1, 其他協(xié)議名稱2>
@end
3.協(xié)議中方法聲明的關(guān)鍵字
1> @required (默認)
要求實現(xiàn)寝衫,如果沒有實現(xiàn)顷扩,會發(fā)出警告
2> @optional
不要求實現(xiàn),這樣不會有警告
4.定義一個變量的時候慰毅,限制這個變量保存的對象遵守某個協(xié)議
類名<協(xié)議名稱> *變量名;
id<協(xié)議名稱> 變量名;
NSObject<MyProtocol> *obj;
id<MyProtocol> obj2;
如果沒有遵守對應的協(xié)議隘截,編譯器會警告
5.@property中聲明的屬性也可用做一個遵守協(xié)議的限制
@property (nonatomic, strong) 類名<協(xié)議名稱> *屬性名;
@property (nonatomic, strong) id<協(xié)議名稱> 屬性名;
@property (nonatomic, strong) Dog<MyProtocol> *dog;
@property (nonatomic, strong) id<MyProtocol> dog2;
6.協(xié)議可用定義在單獨.h文件中,也可用定義在某個類中
1> 如果這個協(xié)議只用在某個類中汹胃,應該把協(xié)議定義在該類中
2> 如果這個協(xié)議用在很多類中婶芭,就應該定義在單獨文件中
7.分類可用定義在單獨.h和.m文件中,也可用定義在原來類中
1> 一般情況下着饥,都是定義在單獨文件
結(jié)構(gòu)體
/*
NSRange(location length)
NSPoint\CGPoint
NSSize\CGSize
NSRect\CGRect (CGPint CGSize)
*/
// 使用這些CGPointEqualToPoint犀农、CGRectContainsPoint等函數(shù)的前提是添加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
字符串的創(chuàng)建
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 : 資源路徑
// 協(xié)議頭://路徑
// file://
// ftp://
字符串的拼接和求字符串的范圍
/*
NSString : 不可變字符串
NSMutableString : 可變字符串
*/
NSMutableString *s1 = [NSMutableString stringWithFormat:@"my age is 10"];
// 拼接內(nèi)容到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);
字符串的導出
SString *str = @"4234234";
NSURL *url = [NSURL fileURLWithPath:@"/Users/apple/Desktop/my2.txt"];
[str writeToURL:url atomically:YES encoding:NSUTF8StringEncoding error:nil];
NSArray
NSArray :不可變數(shù)組
NSMutableArray : 可變數(shù)組
OC數(shù)組不能存放nil值
OC數(shù)組只能存放OC對象宰掉、不能存放非OC對象類型呵哨,比如int、struct轨奄、enum等
這個array永遠是空數(shù)組
NSArray *array = [NSArray array];
1.NSArray的創(chuàng)建
NSArray *array2 = [NSArray arrayWithObject:@"jack"];
// nil是數(shù)組元素結(jié)束的標記
NSArray *array3 = [NSArray arrayWithObjects:@"jack", @"rose", nil];
// [array2 count];
//NSArray *array4 = [NSArray arrayWithObjects:@"jack", @"rose", @"4324324", nil];
// 快速創(chuàng)建一個NSArray對象
NSArray *array4 = @[@"jack", @"rose", @"4324324"];
2.NSArray的元素個數(shù)
NSLog(@"%ld", array3.count);
3.NSArray中元素的訪問
NSLog(@"%@", [array3 objectAtIndex:1]);
// array3[1];
NSLog(@"%@", array3[0]);
NSFileManager-計算代碼行數(shù)
/*
* 考察NSString孟害、NSArray的使用
* NSFileManager
*/
// 計算文件的代碼行數(shù)
/*
path : 文件的全路徑(可能是文件夾、也可能是文件)
返回值 int :代碼行數(shù)
*/
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下面的所有內(nèi)容(文件夾、文件)
NSArray *array = [mgr contentsOfDirectoryAtPath:path error:nil];
// 定義一個變量保存path中所有文件的總行數(shù)
int count = 0;
// 遍歷數(shù)組中的所有子文件(夾)名
for (NSString *filename in array) {
// 獲得子文件(夾)的全路徑
NSString *fullPath = [NSString stringWithFormat:@"%@/%@", path, filename];
// 累加每個子路徑的總行數(shù)
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;
}
// 加載文件內(nèi)容
NSString *content = [NSString stringWithContentsOfFile:path encoding:NSUTF8StringEncoding error:nil];
// 將文件內(nèi)容切割為每一行
NSArray *array = [content componentsSeparatedByString:@"\n"];
// // 刪掉文件路徑前面的/Users/mac/Desktop/Demo/LGOCText/計算代碼行數(shù)/計算代碼行數(shù)/
// NSRange range = [path rangeOfString:@"/Users/mac/Desktop/Demo/LGOCText/計算代碼行數(shù)/計算代碼行數(shù)/"];
// NSString *str = [path stringByReplacingCharactersInRange:range withString:@""];
// 打印文件路徑和行數(shù)
NSLog(@"path:%@, array.count = %ld", path, array.count);
return array.count;
}
}
int main(int argc, const char * argv[]) {
@autoreleasepool {
NSUInteger count = codeLineCount(@"/Users/mac/Desktop/Demo/LGOCText/計算代碼行數(shù)/計算代碼行數(shù)/main.m");
NSLog(@"count = %ld", count);
}
return 0;
}
NSSet
/*
NSSet和NSArray的對比
1> 共同點
* 都是集合,都能存放多個OC對象
* 只能存放OC對象惯雳,不能存放非OC對象類型(基本數(shù)據(jù)類型:int谷誓、char、float等吨凑,結(jié)構(gòu)體,枚舉)
* 本身都不可變,都有一個可變的子類
2> 不同點
* NSArray有順序鸵钝,NSSet沒有順序
*/
NSSet的基本使用
NSSet *s = [NSSet set];
NSSet *s2 = [NSSet setWithObjects:@"jack",@"rose", @"jack2",@"jack3",nil];
// 隨機拿出一個元素
NSString *str = [s2 anyObject];
NSLog(@"%@", str);
NSLog(@"%ld", s2.count);
NSMutableSet
NSMutableSet *s = [NSMutableSet set];
// 添加元素
[s addObject:@"hack"];
// 刪除元素
// [s removeObject:<#(id)#>];
NSDictionary
/*
集合
1.NSArray\NSMutableArray
* 有序
* 快速創(chuàng)建(不可變):@[obj1, obj2, obj3]
* 快速訪問元素:數(shù)組名[i]
2.NSSet\NSMutableSet
* 無序
3.NSDictionary\NSMutableDictionary
* 無序
* 快速創(chuàng)建(不可變):@{key1 : value1, key2 : value2}
* 快速訪問元素:字典名[key]
*/
NSMutableDictionary *dict = [NSMutableDictionary dictionary];
// 添加鍵值對
[dict setObject:@"jack" forKey:@"name"];
[dict setObject:@"北京" forKey:@"address"];
[dict setObject:@"rose" forKey:@"name"];
// 移除鍵值對
[dict removeObjectForKey:<#(id)#>];
/*
字典:
key ----> value
索引 ----> 文字內(nèi)容
里面存儲的東西都是鍵值對
*/
// 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);
// 返回的是鍵值對的個數(shù)
NSLog(@"%ld", dict.count);
// 字典不允許有相同的key糙臼,但允許有相同的value(Object)
// 字典是無序的
NSDictionary *dict = @{
@"address" : @"北京",
@"name" : @"jack",
@"name2" : @"jack",
@"name3" : @"jack",
@"qq" : @"7657567765"};
/*
NSArray *keys = [dict allKeys];
for (int i = 0; i < dict.count; i++) {
NSString *key = keys[i];
NSString *object = dict[key];
NSLog(@"%@ = %@", key, object);
}
*/
[dict enumerateKeysAndObjectsUsingBlock:^(id _Nonnull key, id _Nonnull obj, BOOL * _Nonnull stop) {
NSLog(@"%@ - %@", key, obj);
}];
NSNumber
將各種基本數(shù)據(jù)類型包裝成NSNumber對象
@20 將 20包裝成一個NSNumber對像
@10.5;
@YES;
@'A'; // NSNumber對象
@"A"; // NSString對象
// 將age變量包裝成NSNumber對象
int age = 100;
@(age);
//[NSNumber numberWithInt:age];
NSNumber *n = [NSNumber numberWithDouble:10.5];
double d = [n doubleValue];
int a = 20;
NSString *str = [NSString stringWithFormat:@"%d", a];
NSLog(@"%d", [str intValue]);
void test() {
NSNumber *num = [NSNumber numberWithInt:10];
NSDictionary *dict = @{
@"name" : @"jack",
@"age" : num
};
NSNumber *num2 = dict[@"age"];
int a = [num2 intValue];
NSLog(@"%d" , a);
}
NSDate
void use() {
// 創(chuàng)建一個時間對象
NSDate *date = [NSDate date];
// 打印出的時候是0時區(qū)的時間(北京-東8區(qū))
NSLog(@"%@", date);
NSDate *date2 = [NSDate dateWithTimeInterval:5 sinceDate:date];
// 從1970開始走過的秒數(shù)
NSTimeInterval seconds = [date2 timeIntervalSince1970];
// [date2 timeIntervalSinceNow];
}
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);
}
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;
}
NSValue
// NSNumber之所以能包裝基本數(shù)據(jù)類型為對象,是因為繼承了NSValue
// 結(jié)構(gòu)體--->OC對象
CGPoint p = CGPointMake(10, 10);
// 將結(jié)構(gòu)體轉(zhuǎn)為Value對象
NSValue *value = [NSValue valueWithPoint:p];
// 將value轉(zhuǎn)為對應的結(jié)構(gòu)體
// [value pointValue];
NSArray *array = @[value ];