iOS開發一些細節

block 的注意事項

使用copy修飾屬性網絡

想不循環應用,那麼在 block 外面這樣聲明異步

__weak __typeof(self)weakSelf = self;

接着,在 block 裏面這樣async

__strong __typeof(weakSelf)strongSelf = weakSelf;

這樣既防止循環應用,又避免 block 內部 self 會無效的可能優化

AFNetworking裏面這樣使用的不少atom

 

UITableView 的優化

  • 最基本的,cell 重用機制,若是這個都不知道的話,那能夠去撞牆了。。。spa

  • 異步加載數據,這個也是很基本的線程

  • 自動載入更新數據,好比每次載入20條信息,而後在滾動到最後5條信息,就加載更多的信息code

  • 圖片下載完成之後,判斷 cell 若是是可見的,那麼就須要更新圖像隊列

     

GCD

異步請求,我只認準 GCD | GCD異步,你值得擁有 (廣告先走一波)圖片

網絡請求放在子線程,UI 只能在主線程更新

dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^(void) {
    // 處理各類耗時間的事情,好比網絡請求數據,天知道要何時才能結束
    
    dispatch_async(dispatch_get_main_queue(), ^(void) {
        
        // 好了之後,咱們回到主線程進行界面刷新
    });
});

只執行一次

Xcode 自帶的代碼塊

static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
    
});

延遲執行

Xcode 自帶的代碼塊

double delaySeconds = 5.0;
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(delaySeconds * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
    
});

自定義隊列

dispatch_queue_t baidu_queue = dispatch_queue_create("baidu.com", NULL);

dispatch_async(baidu_queue, ^{
    
});

數據持久化

沙盒

訪問沙盒裏面幾個路徑

//獲取根目錄
NSString *homePath = NSHomeDirectory();
NSLog(@"Home目錄:%@",homePath);
//獲取Documents文件夾目錄
NSArray *docPath = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask, YES);
NSString *documentsPath = [docPath objectAtIndex:0];
NSLog(@"Documents目錄:%@",documentsPath);
//獲取Cache目錄
NSArray *cacPath = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES);
NSString *cachePath = [cacPath objectAtIndex:0];
NSLog(@"Cache目錄:%@",cachePath);
//獲取Library目錄
NSArray *libsPath = NSSearchPathForDirectoriesInDomains(NSLibraryDirectory, NSUserDomainMask, YES);
NSString *libPath = [libsPath objectAtIndex:0];
NSLog(@"Library目錄:%@",libPath);
//獲取temp目錄
NSString *tempPath = NSTemporaryDirectory();
NSLog(@"temp目錄:%@",tempPath);

寫入與讀取

NSArray *testArray1 = @[@"1",@"2"];
//documentsPath是前面建立的
NSString *filePath = [documentsPath stringByAppendingPathComponent:@"testArray1.text"];
//寫入
[testArray1 writeToFile:filePath atomically:YES];
//讀取
NSArray *readTestArray1 = [NSArray arrayWithContentsOfFile:filePath];

文件管理

//獲取文件管理器
NSFileManager *fileManager = [NSFileManager defaultManager];

//文件目錄名
NSString *testDirectory = [documentsPath stringByAppendingPathComponent:@"ttttest"];

//執行建立
[fileManager createDirectoryAtPath:testDirectory withIntermediateDirectories:YES attributes:nil error:nil];

在剛纔建立的文件夾下繼續寫入內容

//在文件目錄下繼續寫入的路徑
NSString *tPath = [testDirectory stringByAppendingPathComponent:@"t1.txt"];

//要寫入內容
NSString *contentString = @"ready...";

//執行寫入
[fileManager createFileAtPath:tPath contents:[contentString dataUsingEncoding:NSUTF8StringEncoding] attributes:nil];

獲取一個目錄下面全部的子文件

//獲取documents下面的全部文件,能夠看到隱藏的內容
NSArray *file = [fileManager subpathsOfDirectoryAtPath:documentsPath error:nil];

NSLog(@"file%@",file);

NSArray *file2 = [fileManager subpathsAtPath:documentsPath];

NSLog(@"file2=%@",file2);

改變文件管理器所能操做的位置

//移到準備操做的目錄下
[fileManager changeCurrentDirectoryPath:[documentsPath stringByExpandingTildeInPath]];

//建立文件
NSString *fileName = @"newTest.txt";

NSString *tString = @"ttttttt";

[fileManager createFileAtPath:fileName contents:[tString dataUsingEncoding:NSUTF8StringEncoding] attributes:nil];

刪除文件

//接着上面,就一句話
[fileManager removeItemAtPath:fileName error:nil];
相關文章
相關標籤/搜索