iOS學習(OC語言)知識點整理
一、單例模式
1)單例是一種編程思想,一個設計模式,與語言無關在采用了單例對象的應用程序中,需要單例類自行提供實例化單例對象,
不管實例化單例對象多少次,只有一個對象存在,這個對象是全局的,能夠被整個應用程序共享,訪問
2)使用單例模式時使用類提供的類方法獲取單例對象,盡量不要用alloc init的方法獲取單例對象
3)單例類提供類方法獲取單例對象時類方法一般以sharedXX/standedXX/defaultXX開頭
實例代碼:
#import "Plane.h"static Plane *plane=nil;//定義一個靜態的全局變量@implementation Plane//獲取單例對象的方法 +(Plane *)sharedPalne{ //解決數據同步(當一個線程訪問時,其他的線程不能操作 @synchronized(self){ if(plane==nil){ plane=[[Plane alloc]init]; } } return plane;}//當調用alloc方法時會執行此方法 +(id)allocWithZone:(struct _NSZone *)zone{ NSLog(@"%@",NSStringFromSelector(_cmd)); if(plane==nil){ plane=[super allocWithZone:zone]; } return plane;}@end
二、NSFileManager 文件/文件夾 的操作
1)NSFileManager 用于對文件或目錄(文件夾)管理的單例類。
2)文件管理類的實例化方法 例如:
1 NSFileManager *fm=[NSFileManager defaultManager];
3)fileExistsAtPath 用于判斷文件是否存在 例如:
1 BOOL ret=[fm fileExistsAtPath:@"/Users/kingkong/Desktop/Test/test.txt"];
4)createFileAtPath 用于創建文件 第一個參數是文件名,第二個參數是文件的初始值,第三個參數是文件的屬性 例如:
1 BOOL ret=[fm createFileAtPath:@"/Users/kingkong/Desktop/Test/test.txt" contents:nil attributes:nil];
5)copyItemAtPath 用于復制文件:將某一個文件復制成另一個文件,第三個參數:回調錯誤信息 例如:
1 NSError *error=nil;//用于接收錯誤信息 3 NSString *curpath=@"/Users/kingkong/Desktop/Test/test.txt”; 5 NSString *topath=@"/Users/kingkong/Desktop/Test/test2.txt”; 7 ret=[fm copyItemAtPath: curpath toPath: topath error:&error]; 9 if(error!=nil){ 11 NSLog(@"error:%@",[error userInfo]); 13 }
6)moveItemAtPath 用于移動/重命名文件;將某個路徑下的文件移動到另一個路徑下的文件(目標路徑一定要指定文件名),
如果源路徑與目 標路徑下的文件名不同,同時重命名 例如:
1 NSString *curpath=@"/Users/kingkong/Desktop/Test/test.txt”;2 3 NSString *topath=@"/Users/kingkong/Desktop/Test/test2.txt”;4 5 [fm moveItemAtPath: curpath toPath: topath error:nil];
7)attributesOfItemAtPath 獲取文件的屬性 例如:
1 NSString *curpath=@"/Users/kingkong/Desktop/Test/test.txt";2 3 NSDictionary *dict= [fm attributesOfItemAtPath: curpath error:nil];4 5 //獲取屬性字典中該文件的大小6 7 NSLog(@"size:%@",[dict objectForKey:NSFileSize]);
8)removeItemAtPath 用于刪除指定路徑下的文件 例如:
1 NSString *path=@"/Users/kingkong/Desktop/Test/test.txt”;2 3 BOOL ret=[fm removeItemAtPath: path error:nil];
9)NSData:當對文件操作(寫數據到文件和從文件中讀數據)和從網絡上獲取數據,需要將數據轉換為純粹的二進制形式,
NSData就是二進制形式的緩存對象,相當于C中的char buf[255] 例如:
1 NSString *path=@"/Users/kingkong/Desktop/Test/test.txt”;2 3 NSData *content=[fm contentsAtPath: path];
10)NSData數據轉換為NSString類型數據 例如:
1 //NSData---->NSString,將NSData對象轉換為字符串2 3 NSString *scontent=[[NSString alloc]initWithData:content encoding:NSUTF8StringEncoding];
11)NSString類型的數據轉換為NSData數據 例如:
1 NSString *str=@"hello China!";2 3 //NSString---->NSData;將字符串轉換為NSData數據(寫入文件或傳輸到網絡上)4 5 NSData *data2=[str dataUsingEncoding:NSUTF8StringEncoding];
12)createFileAtPath … contents 建文件并指定寫入內容 例如:
1 NSString *path=@"/Users/kingkong/Desktop/Test/test.txt”;2 3 [fm createFileAtPath:path contents:data2 attributes:nil];
13)fileExistsAtPath… isDirectory 判斷目錄是否存在(在文件/目錄存在的情況下,通過第二個參數獲取是否是目錄/文件) 例如:
1 BOOL isDir;//可能結果有:-1,0,12 3 NSString *path=@"/Users/kingkong/Desktop/Test/test.txt”;4 5 BOOL ret=[fm fileExistsAtPath:path isDirectory:&isDir];
14)contentsOfDirectoryAtPath 淺遍歷:獲取目錄下的子目錄和文件名 例如:
1 NSString *path=@"/Users/kingkong/Desktop/Test”;2 3 NSArray *array=[fm contentsOfDirectoryAtPath: path error:nil];4 5 NSLog(@"array%@",array);
15)subpathsAtPath 深遍歷:獲取目錄下所有的內容(包括子目錄下的所有的內容) 例如:
1 NSString *path=@"/Users/kingkong/Desktop/Test”;2 3 NSArray *array2=[fm subpathsAtPath:path];4 5 NSLog(@"array2:%@",array2);
16)currentDirectoryPath 獲取當前應用程序所在目錄 例如:
1 NSString* currentPath=[fm currentDirectoryPath];2 3 NSLog(@"%@",currentPath);
17)createDirectoryAtPath… withIntermediateDirectories 創建目錄,第一個參數是目錄名,第二個參數是
當目錄不存在時是否需要創建其上一級不存在的目錄(中間目錄)
1 NSString *path=@"/Users/kingkong/Desktop/Test/File”;2 3 BOOL ret=[fm createDirectoryAtPath:path withIntermediateDirectories:YES attributes:nil error:nil];
三、NSFileHandle 對文件進行讀寫操作
1)fileHandleForReadingAtPath 創建一個只讀文件的對象(句柄) 例如:
1 NSString *path=@"/Users/kingkong/Desktop/Test/test.txt”;2 3 NSFileHandle *readHandle=[NSFileHandle fileHandleForReadingAtPath: path];
2)readDataOfLength 讀取文件中指定長度的內容 例如:
NSData *data=[readHandle readDataOfLength:3];NSString *sdata=[[NSString alloc]initWithData:data encoding:NSUTF8StringEncoding];NSLog(@"%@",sdata);
3)readDataToEndOfFile 從當前位置讀取內容到文件結束 例如:
1 data=[readHandle readDataToEndOfFile];2 3 sdata=[[NSString alloc]initWithData:data encoding:NSUTF8StringEncoding];4 5 NSLog(@"%@",sdata);6 7 [readHandle closeFile];//讀取文件內容結束(注意如果上次讀取過沒關閉再次讀取時會從上次讀取結束位置開始讀取)
4)fileHandleForWritingAtPath 創建一個只寫文件的對象 例如:
1 NSFileHandle *writeHandle=[NSFileHandle fileHandleForWritingAtPath:path"]; 2 3 NSString *s1=@"hello"; 4 5 //seekToEndOfFile 將對象指針移到文件末尾(實現追加內容) 6 7 [writeHandle seekToEndOfFile]; 8 9 [writeHandle writeData:[s1 dataUsingEncoding:NSUTF8StringEncoding]];10 11 //synchronizeFile將數據同步寫入文件中12 13 [writeHandle synchronizeFile];14 15 [writeHandle closeFile];//表明操作結束
5)truncateFileAtOffset 從指定位置字符開始截斷文件內容,保留多少個字符,設置為0,再寫入相當于清空重寫 例如:
1 [writeHandle truncateFileAtOffset:4];
6)seekToFileOffset 從指定位置字符開始寫入文件,覆蓋部分內容 例如:
1 [writeHandle seekToFileOffset:2];
四、關于Path (文件路徑)的操作
1)pathComponents 獲取路徑的各部分 例如:
1 NSString *path=@"/Users/kingkong/Desktop/Test/text.txt”;2 3 NSArray *array1= [path pathComponents];4 5 NSLog(@"array1:%@",array1);
2)pathExtension 獲取文件后綴名 例如:
1 NSLog(@"%@",[path pathExtension]);//結果:txt
3)isAbsolutePath 判斷是否是絕對路徑:以/開頭的路徑 例如:
1 NSLog(@"%d",[path isAbsolutePath]);//結果:1
4)lastPathComponent 獲取路徑的最后一部分 例如:
1 NSLog(@"last:%@",path.lastPathComponent);//結果:text.txt
5)stringByAppendingPathComponent 在一個路徑后添加另一個路徑 例如:
1 NSLog(@"%@",[path stringByAppendingPathComponent:@"files”]);2 3 //結果:/Users/kingkong/Desktop/Test/text.txt/files
6)stringByAppendingPathExtension 為路徑添加擴展名 例如:
1 NSLog(@"%@",[path stringByAppendingPathExtension:@"m"]);2 3 //結果:/Users/kingkong/Desktop/Test/text.txt.m
新聞熱點
疑難解答