日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

OC高级foundation框架类以及数据的简单存储

發布時間:2024/3/12 编程问答 31 豆豆
生活随笔 收集整理的這篇文章主要介紹了 OC高级foundation框架类以及数据的简单存储 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
Objective-c高級復習

第一章 Foundation框架介紹
Foundation.framework是iOS開發的核心框架之

第二章

一、NSNumber OC的數字類型(存儲C語言中簡單的基本數據類型)
C語言中簡單的基本數據類型:int、float、double、char、bool
C語言中復雜的基本數據類型:enum、struct、*p(NSValue)
1 創建對象(將C->OC)
1.1 使用常量的方式創建NSNumber對象
NSNumber *number1 = @69;

float num1 = 99.8f;
NSNumber *number2 = @(num1);

1.2 使用靜態(類)方法創建NSNumber對象(常用)
NSNumber *number3 = [NSNumber numberWithInteger:110];

BOOL flag1 = YES;
NSNumber *number4 = [NSNumber numberWithBool:flag1];

1.3 使用初始化方法創建NSNumber對象
NSNumber *number5 = [[NSNumber alloc] initWithInteger:110];

2 使用

2.1 將NSNumber對象轉換為C語言基本數據類型
float num2 = [number2 floatValue];
BOOL flag2 = [number4 boolValue];

2.2 將NSNumber對象轉換為NSString對象
NSString *string = [number5 stringValue];

2.3 判斷兩個NSNumber對象值是否相等
if ([number3 isEqualToNumber:number5]) {
NSLog(@”相等”);
}else{
NSLog(@”不等”);
}

2.4 比較兩個NSNumber對象值的大小
NSNumber *number6 = [NSNumber numberWithInteger:100];
NSNumber *number7 = [NSNumber numberWithInteger:100];

NSComparisonResult 枚舉
NSOrderedAscending = -1L, 升序
NSOrderedSame, 相同
NSOrderedDescending 降序

NSComparisonResult result = [number6 compare:number7];//100 90

注意:
1.使用常量的方式創建NSNumber對象,不需要管理內存(系統對NSNumber的引用計數做了特殊處理)
2.使用靜態方法創建NSNumber對象,不需要管理內存(通過靜態方法創建的對象,系統會將對象的所有權交給自動釋放池進行管理)
3.使用初始化方法創建NSNumber對象,不需要管理內存(系統對NSNumber的引用計數做了特殊處理)
4.一般使用靜態方法來創建NSNumber對象,不使用初始化方法
5.判斷兩個NSNumber是否相等使用isEqualToNumber:方法,判斷兩個NSNumber的大小使用compare:方法

二、NSString 不可變字符串
1 創建
1.1 使用常量方式創建NSString對象 (C -> OC)
NSString *string1 = Objective @” -C”;

1.2 使用靜態(類)方法創建NSString對象
char *s = “iOS”;
NSString *string2 = [NSString stringWithCString:s encoding:NSUTF8StringEncoding];
NSString *string3 = [NSString stringWithFormat:@”%s%i%@”,s,9,@”手機操作系統”];格式化字符串 (常用)

1.3 使用初始化方法創建NSString對象
NSString *string4 = [[NSString alloc] initWithString:@”Objective-C 2.0”];
NSString *str = [[NSString alloc] initWithData:[NSData data] encoding:NSUTF8StringEncoding];

2 常用使用
2.1 獲取字符串的長度
NSString *string5 = @”hello world!”;
NSUInteger len = [string5 length];
2.2 根據下標獲取指定單個字符
unichar ch = [string5 characterAtIndex:2];

3 判斷字符串
3.1 判斷兩個字符串是否相等
isEqualToString:

3.2 判斷字符串是否以指定內容開頭
hasPrefix:

3.3 判斷字符串是否以指定內容結尾
hasSuffix:

4.字符串大小寫的轉換

4.1 小寫轉大寫
NSString *string11 = [string10 uppercaseString];

4.2 大寫轉小寫
NSString *string12 = [string10 lowercaseString];

4.3 首字母大寫
NSString *string13 = [string10 capitalizedString];

5.截取字符串
NSString *string14 = @”Today is qixi day”;

5.1 截取字符串從指定位置到末尾(包含指定位置)
NSString *string15 = [string14 substringFromIndex:5];

5.2 截取字符串從開始位置到指定位置(不包含指定位置)
NSString *string16 = [string14 substringToIndex:5];

5.3 截取指定范圍的字符串
NSString *string17 = [[string14 substringFromIndex:9] substringToIndex:4];
NSRange 結構體 表示范圍(location 位置 length 長度)

NSRange range = NSMakeRange(9, 4);
NSString *string18 = [string14 substringWithRange:range];

6 分割字符串
NSString *string19 = @”范菲菲\n方芳芳 傅芬芳 鳳飛飛”;

6.1 按指定字符串進行字符串分割
NSArray *array1 = [string19 componentsSeparatedByString:@” “];

6.2 按指定字符集合進行字符串分割
NSCharacterSet 字符集合對象

whitespaceCharacterSet 空格
newlineCharacterSet 換行
whitespaceAndNewlineCharacterSet 空格+換行

NSCharacterSet *set = [NSCharacterSet whitespaceAndNewlineCharacterSet];
NSArray *array2 = [string19 componentsSeparatedByCharactersInSet:set];

7 去字符串首尾的空格和換行
NSString *string20 = @” whitespaceCharacterSet \n newlineCharacterSet \n”;

NSString *string21 = [string20 stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];

8 替換字符串
NSString *string22 = @”one two three four five three”;

8.1 替換指定字符串
NSString *string23 = [string22 stringByReplacingOccurrencesOfString:@”o” withString:@”@”];

8.2 替換指定范圍的字符串
string22 = [string22 stringByReplacingCharactersInRange:NSMakeRange(8, 5) withString:@”eight”];

9 查找字符串位置
NSString *string24 = @”one four two three four five”;

NSRange range2 = [string24 rangeOfString:@”four”];

if (range2.length>0||range2.location != NSNotFound)

10 追加字符串
NSString *string25 = @”Today is”;

10.1 追加指定字符串
NSString *string26 = [string25 stringByAppendingString:@” Hot Day!”];

10.2 追加格式化的字符串
NSString *string27 = [string26 stringByAppendingFormat:@”–%i”,2015];

10.3 追加文件路徑
path = [path stringByAppendingPathComponent:@”apple”];

10.4 追加文件后綴
path = [path stringByAppendingPathExtension:@”txt”];

11 將字符串轉換為C語言的基本類型
NSString *string28 = @”9.9…99”;

CGFloat num1 = [string28 doubleValue]; 9.9
NSInteger num2 = [string28 integerValue]; 9

12 文件讀寫

12.1 寫文件

參數:
file : 寫入文件的路徑
atomically : 是否考慮線程安全(默認:NO)
encoding : 編碼集(默認:NSUTF8StringEncoding)
error : 錯誤
返回值:
YES 寫入成功
NO 寫入失敗

BOOL flag = [content writeToFile:path1 atomically:NO encoding:NSUTF8StringEncoding error:nil];

12.2 讀取文件
NSString *content = [NSString stringWithContentsOfFile:path1 encoding:NSUTF8StringEncoding error:nil];

NSMutableString的用法

1 創建對象

1.1 使用類方法創建
NSMutableString *string2 = [NSMutableString stringWithCapacity:0];
NSMutableString *string3 = [NSMutableString stringWithFormat:@”%i”,100];

1.2 使用初始化方法創建 (常用)
NSMutableString *string4 = [[NSMutableString alloc] initWithCapacity:0];

2 使用
2.1 設置字符串
[string4 setString:@”apple”];

2.2 插入字符串
[string4 insertString:@”android ” atIndex:0];

2.3 追加字符串
[string4 appendString:@” windowsPhone”];
[string4 appendFormat:@”%i-%i”,9,9];

2.4 刪除
[string4 deleteCharactersInRange:NSMakeRange(0, 8)];

2.5 替換
[string4 replaceCharactersInRange:NSMakeRange(0,5) withString:@”蘋果”];

注意:
1.創建NSMutableString一般不使用常量賦值的方式(不能使用子類指針指向父類的成員)
2.一般常用初始化方法創建NSMutableString對象
3.NSMutableString繼承自NSString(NSMutableString擁有NSString的所有方法)

四、NSArray 不可變數組
1.1 常量方式
NSArray *array1 = @[@”oooopppp”,@8989,[NSNull null]];

1.2 類方法
NSArray *array2 = [NSArray arrayWithObjects:@”1”,@”2”,@1,@2, nil];
NSArray *array3 = [NSArray arrayWithArray:array1];
NSArray *array4 = [NSArray arrayWithContentsOfFile:path];

1.3 初始化
NSArray *array3 = [[NSArray alloc] initWithObjects:@”january”,@”february”,@”march”,@”april”,@”may”,@”june”,@”july”,@”auguest”,@”september”,@”octomber”,@”november”,@”december”, nil];

2 使用常用方法

2.1 得到數組個數
NSUInteger count = [array3 count];

2.2 根據下標獲取元素
id obj1 = [array3 objectAtIndex:7];

3 遍歷數組的三種方式

3.1 for循環
for (int i=0; i<count; i++) {
NSString *s = [[array3 objectAtIndex:i] capitalizedString];
}

3.2 枚舉(NSEnumerator)
將數組轉換為枚舉對象
NSEnumerator *em1 = [array3 objectEnumerator];
id obj2;
while (obj2 = [em1 nextObject]) {
if ([obj2 isEqualToString:@”auguest”]) {
NSLog(@”當前是八月份”);
break;
}
}

反轉枚舉
NSEnumerator *em2 = [array3 reverseObjectEnumerator];
id obj3;
while (obj3 = [em2 nextObject]) {
NSLog(@”obj3:%@”,[obj3 uppercaseString]);
}

3.3 快速枚舉(前提:數組中元素類型一致,不需使用下標)
for (NSString *s in array3) {
NSLog(@”s:%@”,s);
}

4 獲取數組的最后一個元素
NSString *string1 = [array3 lastObject];
NSString *string2 = [array3 objectAtIndex:[array3 count]-1];

5.判斷指定元素在原數組中是否存在
if ([array3 containsObject:@”may”]) {
NSLog(@”YES”);
}else{
NSLog(@”NO”);
}

6.獲取元素在數組中的下標
NSUInteger index = [array3 indexOfObject:@”auguest”];

7.連接數組
NSString *string3 = [array3 componentsJoinedByString:@”|”];

8.追加數組
NSArray *array4 = [array3 arrayByAddingObject:@”十三月”];
NSArray *array5 = [array3 arrayByAddingObjectsFromArray:array1];

9.排序
NSString排序:按照字符串的ASCII進行升序排序
NSNumber排序:按照對象值的大小進行升序排序
NSArray *array8 = [array6 sortedArrayUsingSelector:@selector(compare:)];

10 讀寫文件(數組以plist文件形式進行文件存儲)

10.1 寫文件
NSString *path = [[[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0] stringByAppendingPathComponent:@”apple”] stringByAppendingPathExtension:@”plist”];

[array6 writeToFile:path atomically:NO];

10.2 讀文件
NSArray *array10 = [NSArray arrayWithContentsOfFile:path];

五、NSMutableArray 可變數組

1 創建

1.1 類方法
NSMutableArray *array1 = [NSMutableArray arrayWithCapacity:0];
NSMutableArray *array2 = [NSMutableArray arrayWithObjects:@”two”,@”three”,@”five”,@”four”,@”six”,@”eight”, nil];

1.2 初始化方法
NSMutableArray *array3 = [[NSMutableArray alloc] initWithCapacity:0];

2 操作
2.1 添加元素
[array3 addObject:@”one”];
[array3 addObjectsFromArray:array2];

2.2 插入元素
[array3 insertObject:@”zero” atIndex:0];

2.3 替換
[array3 replaceObjectAtIndex:2 withObject:@”2”];

2.4 交換元素的位置
[array3 exchangeObjectAtIndex:0 withObjectAtIndex:array3.count-1];

2.5 排序
[array3 sortUsingSelector:@selector(compare:)];

3 移除
3.1 根據下標移除元素
[array3 removeObjectAtIndex:0];

3.2 移除指定元素
[array3 removeObject:@”four”];

3.3 移除最后一個元素
[array3 removeLastObject];

3.4 移除指定區域的元素
NSRange range1 = NSMakeRange(1, 2);
[array3 removeObjectsInRange:range1];

NSNull、nil、Nil、NULL的區別

1.NSNull:在集合中表示空對象([NSNull null])
NSArray *ay = [NSArray arrayWithObjects:@”aa”,@28,[NSNull null],@”bvb”,nil];

2.nil : 表示空的OC實例對象,表示集合的結束
id obj = nil;

3.Nil : 表示空的OC類對象
Class cls = Nil;

4.NULL C語言的空指針

六、NSDictionary 字典、詞典(java:Hashmap)
鍵值對

1 創建對象
1.1 常量
NSDictionary *dic1 = @{@”key1” : @”value1”,@”key2”:@”value2”};

1.2 類方法
NSDictionary *dic2 = [NSDictionary dictionaryWithObjectsAndKeys:@”v1”,@”k1”,@”v2”,@”k2”,@”v3”,@”k3”, nil];

NSArray *keys = [NSArray arrayWithObjects:@”k1”,@”k2”,@”k3”, nil];
NSArray *objects = [NSArray arrayWithObjects:@”v1”,@”v2”,@”v3”, nil];
NSDictionary *dic3 = [NSDictionary dictionaryWithObjects:objects forKeys:keys];

1.3 初始化
NSDictionary *dic4 = [[NSDictionary alloc] initWithDictionary:dic1];
NSDictionary *dic4 = [[NSDictionary alloc] initWithObjectsAndKeys:@”范菲菲”,@”key1”,@”傅芬芳”,@”key2”,@”鳳飛飛”,@”key3”, nil];

2 常用方法的使用

2.1 得到字典中鍵值對的個數
NSUInteger count = [dic4 count];

2.2 根據鍵獲取值
NSString *key = @”key3”;
NSString *obj = [dic4 objectForKey:key];

2.3 枚舉(得到所有的鍵)
NSEnumerator *em = [dic4 keyEnumerator];
//NSEnumerator *em = [dic4 objectEnumerator];枚舉所有的值(少用)

id obj2;
while (obj2 = [em nextObject]) {
NSLog(@”%@->%@”,obj2,[dic4 objectForKey:obj2]);
}

NSArray *ks = [[dic4 allKeys] sortedArrayUsingSelector:@selector(compare:)];
//NSArray *ks = [dic4 keysSortedByValueUsingSelector:@selector(compare:)];
//NSArray *vs = [dic4 allValues];//得到所有的值(少用)
for (NSString *s in ks){
NSLog(@”%@->%@”,s,[dic4 objectForKey:s]);
}

2.4 判斷兩個字典是否相等
if ([dic3 isEqualToDictionary:dic2]) {
NSLog(@”same”);
}else{
NSLog(@”diff”);
}

2.5 讀寫文件

操作路徑
NSString *path = [[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0] stringByAppendingPathComponent:@”apple.plist”];

寫文件
if([dic4 writeToFile:path atomically:NO]){
NSLog(@”write success”);
}else{
NSLog(@”failed”);
}

讀文件
NSDictionary *dic5 = [NSDictionary dictionaryWithContentsOfFile:path];

注意:
1.NSDictionary只能存儲OC類型的對象
2.在NSDictionary中鍵值是一一對應的(一個鍵對應一個值)
3.在NSDictionary中,鍵是唯一的(不能重復)使用NSString表示,值可以重復
4.在NSDictionary中,只能使用鍵訪問值
5.NSDictionary是一個無序的集合(不能使用下標訪問元素)

七、NSMutableDictionary 可變字典

1.創建
NSMutableDictionary *dic1 = [NSMutableDictionary dictionaryWithObjectsAndKeys:@”范菲菲”,@”key1”,@”傅芬芳”,@”key2”,@”鳳飛飛”,@”key3”, nil];

NSMutableDictionary *dic2 = [[NSMutableDictionary alloc] initWithCapacity:10];

2 使用

2.1 添加
[dic2 addEntriesFromDictionary:dic1];

2.2 設置方法(如果key存在則修改,如果不存在則添加)
[dic2 setObject:@”apple” forKey:@”key2”];
[dic2 setObject:@”android” forKey:@”key5”];

2.3 根據key移除指定元素
[dic2 removeObjectForKey:@”key3”];

2.4 根據key的數組移除元素
NSArray *keys = @[@”key1”,@”key5”];
[dic2 removeObjectsForKeys:keys];

2.5 移除所有元素
[dic2 removeAllObjects];

任務:
1.使用isEqualToArray: 、isEqualToDictionary:方法
2.練習NSDictionary、NSMutableDicationary
3.自學NSSet、NSMutableSet

第四章 內存管理 MRC(手動引用計數)

1.內存
組成:代碼區、數據區

數據區4個組成:
常量區
全局、靜態區
棧區 先進后出(成員變量、局部變量、參數等)
堆區 無序(通過malloc、alloc創建的對象)

int num = 10;
NSObject *obj = [[NSObject alloc] init];

注意:
內存管理只需管理堆區的內存

2.指針
作用:用于存儲對象在內存中的首地址

地址->值
0x1111FFFAAA22->二進制

3.引用計數 retainCount

函數:
alloc:設置對象引用計數為1
retain:引用計數+1
release:引用計數立即-1
autorelease:引用計數延遲-1
dealloc:引用計數為0時,系統自動調用銷毀對象

4.自動釋放池 NSAutoreleasePool
編譯指令:@autorelease{}

5.屬性的內存管理
assign、copy、retain
assign C語言類型
copy NSString
retain OC對象

6.深拷貝與淺拷貝
深拷貝:復制對象本身(復制內存)->copy
淺拷貝:復制引用指針(復制變量)->assign/retain

注意:
OC的內存管理是基于對象的引用計數

第五章 ARC 自動引用計數
全稱:Autoamtic Reference Counting

作用:
1.成員變量->編譯器會在dealloc方法中為它添加release操作
2.局部變量->編譯器會在方法結束之前為它添加release操作
3.返回值->編譯器會在return之前為它添加autorelease操作

1.將MRC代碼轉換為ARC代碼

1.1 手動操作
選中項目->Build Settings->ALL->Apple LLVM compiler 4.1 - Language -> Objective-C Autoamtic Reference Counting -> YES ->將源碼中出現retain/release/autorelease/dealloc的代碼刪除掉

1.2 自動操作
菜單->Edit->Refactor->Convert To Objective-C ARC

2.在ARC中使用MRC的代碼
選中項目->Build Phases->Compile Sources->Compiler Flags->-fno-objc-arc

3.強引用(strong)與弱引用(weak)

3.1 屬性中的用法
assign: 用于C語言類型
copy:用于NSString
strong:用于所有OC對象
weak:用于id類型對象

@property (nonatomic,assign)NSInteger num; (默認)
@property (nonatomic,copy)NSString *str;
@property (nonatomic,strong)NSObject *obj;
@property (nonatomic,weak)id delegate;

3.2 變量中的用法
__strong 用于所有OC對象 (默認)
__weak 用于id類型對象

注意:
1.MRC與ARC的內存管理原則是一致,都是基于對象的引用計數。
2.ARC中將內存管理交給編譯器完成
3.strong和weak只能作用在OC對象的屬性上

第六章 歸檔和序列化,解檔和反序列化

一、NSData 字節對象

二、NSKeyedArchiver 歸檔對象

1.歸檔單個對象操作
NSArray *array = @[@”方法”,@”333”,@88,@YES];
BOOL flag = [NSKeyedArchiver archiveRootObject:array toFile:path];

2.歸檔多個對象操作
2.1 指定歸檔文件的路徑
NSString *path = [[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0] stringByAppendingPathComponent:@”file.archiver”];

2.2 創建一個可變的字節對象
NSMutableData *data = [NSMutableData data];

2.3 創建歸檔對象
NSKeyedArchiver *archiver = [[NSKeyedArchiver alloc] initForWritingWithMutableData:data];

2.4 歸檔指定對象
[archiver encodeInteger:88 forKey:@”tmep_integer”];
[archiver encodeObject:@”黃鑫” forKey:@”temp_string”];
[archiver encodeObject:@[@”apple”,@”banana”,@”xigua”] forKey:@”temp_array”];

2.5 結束歸檔(將指定對象轉換為字節對象存入data中)
[archiver finishEncoding];

2.6 將字節對象寫入文件
[data writeToFile:path atomically:NO];

三、NSKeyedUnArchvier 解檔對象

  • 解檔單個對象操作
    NSString *path3 = [[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0] stringByAppendingPathComponent:@”file4.txt”];
    通過NSKeyedUnarchiver解檔對象
    id obj = [NSKeyedUnarchiver unarchiveObjectWithFile:path3];
  • 2.解檔多個對象操作
    2.1 路徑
    NSString *path = [[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0] stringByAppendingPathComponent:@”file.archiver”];

    2.2 讀取文件
    NSData *data = [NSData dataWithContentsOfFile:path];

    2.3 創建解檔對象
    NSKeyedUnarchiver *unarchiver = [[NSKeyedUnarchiver alloc] initForReadingWithData:data];

    2.4 根據鍵解檔指定對象
    NSInteger age = [unarchiver decodeIntegerForKey:@”tmep_integer”];
    NSString *name = [unarchiver decodeObjectForKey:@”temp_string”];
    NSArray *array = [unarchiver decodeObjectForKey:@”temp_array”];

    2.5 結束解檔
    [unarchiver finishDecoding];

    四、NSCoding協議
    解檔方法
    - (id)initWithCoder:(NSCoder *)aDecoder{
    self = [super init];
    if (self) {
    _age = [aDecoder decodeIntegerForKey:AGE];
    _name = [[aDecoder decodeObjectForKey:NAME] copy];
    //_obj = [[aDecoder decodeObjectForKey:OBJ] copy];
    }
    return self;
    }

    歸檔方法
    - (void)encodeWithCoder:(NSCoder *)aCoder{
    [aCoder encodeInteger:_age forKey:AGE];
    [aCoder encodeObject:_name forKey:NAME];
    //[aCoder encodeObject:_obj forKey:OBJ];
    }

    五、NSCoping 協議
    復制方法
    - (id)copyWithZone:(NSZone *)zone{
    Student *stu = [[[self class] allocWithZone:zone] init];
    stu.age = _age;
    stu.name = [_name copyWithZone:zone];

    return stu;

    }

    第八章 多線程

    線程、進程、應用程序之間的關系

    應用程序:存儲在硬盤中的程序
    進程:存儲在內存中的(是應用程序在內存中的體現)
    線程:線程是進程中的基本單位

    注意:
    1.進程之間是相互對立的存在
    2.進程是由多個線程共同操作完成
    3.iOS中的main函數啟動主線程,默認所有的操作都運行在主線程上
    4.線程是按照時間順序執行的(同一時間只能執行一個事情)

    一、啟動線程的方式
    1.使用NSThread自動啟動自定義線程
    [NSThread detachNewThreadSelector:@selector(run1) toTarget:self withObject:nil];

    2.使用NSThread手動啟動自定義線程
    NSThread *thread = [[NSThread alloc] initWithTarget:self selector:@selector(run2) object:nil];
    啟動線程
    [thread start];

    3.自動啟動一個后臺(異步)線程
    [self performSelectorInBackground:@selector(run3) withObject:nil];

    4.NSTimer 定時器
    4.1 創建一個定時器
    timeInterval:時間間隔
    target:目標
    selector:選擇器方法
    userInfo:參數
    repeats:是否重復
    [NSTimer scheduledTimerWithTimeInterval:1.0 target:self selector:@selector(showTime:) userInfo:nil repeats:YES];

    4.2 銷毀定時器
    [timer invalidate];

    4.3 在指定時間銷毀定時器
    [timer setFireDate:[date dateByAddingTimeInterval:10]];

    5.GCD
    Grand Central Dispatch (GCD)是Apple開發的一個多核編程的解決方法。

    創建一個異步線程
    dispatch_async(dispatch_get_global_queue(0, 0), ^{
    //訪問網絡圖片

    NSURL *url = [NSURL URLWithString:@"http://desk.fd.zol-img.com.cn/g5/M00/02/07/ChMkJlXmaS2IHn4OAACKArgFkSwAACJjgHi-dUAAIoa000.jpg"]; NSData *data = [NSData dataWithContentsOfURL:url]; UIImage *img = [UIImage imageWithData:data];//回到主線程,更新UI dispatch_async(dispatch_get_main_queue(), ^{UIImageView *imgView = (UIImageView *)[self.view viewWithTag:10];imgView.image = img; });

    });

    二、線程之間的通信

    實現通信:回到主線程,更新UI
    [self performSelectorOnMainThread:@selector(updateLbl2) withObject:nil waitUntilDone:YES];

    1.主線程又叫UI線程(作用:負責界面展示) ->只有主線程能夠操作界面
    2.線程與線程之間數據是共享的

    三、 NSCondition 瑣
    作用:用于處理多線程中線程安全

    1.創建
    NSCondition *condition = [[NSCondition alloc] init];

    2.加鎖
    [condition lock];

    3.線程的操作
    …..

    4.解鎖
    [condition unlock];

    總結

    以上是生活随笔為你收集整理的OC高级foundation框架类以及数据的简单存储的全部內容,希望文章能夠幫你解決所遇到的問題。

    如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。