国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 學院 > 開發設計 > 正文

IOS開發基礎知識--碎片16

2019-11-14 18:50:32
字體:
來源:轉載
供稿:網友

 

1:Objective-C語法之動態類型(isKindOfClass, isMemberOfClass,id)

 

對象在運行時獲取其類型的能力稱為內省。內省可以有多種方法實現。判斷對象類型-(BOOL) isKindOfClass: classObj判斷是否是這個類或者這個類的子類的實例-(BOOL) isMemberOfClass: classObj 判斷是否是這個類的實例實例一:   Person *person = [[Person alloc] init];      //父類   Teacher *teacher = [[Teacher alloc] init];  //子類        //YES      if ([teacher isMemberOfClass:[Teacher class]]) {          NSLog(@"teacher Teacher類的成員");     }     //NO      if ([teacher isMemberOfClass:[Person class]]) {         NSLog(@"teacher Person類的成員");     }     //NO      if ([teacher isMemberOfClass:[NSObject class]]) {         NSLog(@"teacher NSObject類的成員");     }  實例二:Person *person = [[Person alloc] init];  Teacher *teacher = [[Teacher alloc] init];    //YES   if ([teacher isKindOfClass:[Teacher class]]) {      NSLog(@"teacher 是 Teacher類或Teacher的子類");  }  //YES   if ([teacher isKindOfClass:[Person class]]) {      NSLog(@"teacher 是 Person類或Person的子類");  }  //YES   if ([teacher isKindOfClass:[NSObject class]]) {      NSLog(@"teacher 是 NSObject類或NSObject的子類");  }  isMemberOfClass判斷是否是屬于這類的實例,是否跟父類有關系他不管,所以isMemberOfClass指到父類時才會為NO;對方法進行判斷:-(BOOL) respondsToSelector: selector 判讀實例是否有這樣方法+(BOOL) instancesRespondToSelector:  判斷類是否有這個方法。此方法是類方法,不能用在類的對象實例三:// YES   teacher是對象if ( [teacher respondsToSelector: @selector( setName: )] == YES ) {      NSLog(@"teacher responds to setSize: method" );  } // YES   Teacher是類if ( [Teacher instancesRespondToSelector: @selector(teach)] == YES ) {      NSLog(@"Teacher instance responds to teach method");  }  

2:IOS 開發中判斷字符串是否為空字符的方法

- (BOOL) isBlankString:(NSString *)string {    if (string == nil || string == NULL) {        return YES;    }    if ([string isKindOfClass:[NSNull class]]) {        return YES;    }    if ([[string stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceCharacterSet]] length]==0) {        return YES;    }    return NO;} 

3:刪除Caches文件夾的內容

// 文件管理者NSFileManager *mgr = [NSFileManager defaultManager];// 緩存路徑NSString *caches = [NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES) lastObject];[mgr removeItemAtPath:caches error:nil];

4:計算某個文件夾或文件的大小

/** *  @ 15-06-17 09:06:22 * *  @brief  計算文件或文件夾的大小 因為osx 文件夾是沒有大小這個屬性 要通過各個文件計算得到 subpathsAtPath可以獲得文件夾下面所有的文件 包含子文件夾里面 *  @param filePath 比如緩存caches的路徑 *  @return 大小 */- (NSInteger)fileSize:(NSString *)filePath{    NSFileManager *mgr = [NSFileManager defaultManager];    // 判斷是否為文件    BOOL dir = NO;    BOOL exists = [mgr fileExistsAtPath:filePath isDirectory:&dir];    // 文件/文件夾不存在    if (exists == NO) return 0;        if (dir) { // self是一個文件夾        // 遍歷caches里面的所有內容 --- 直接和間接內容        NSArray *subpaths = [mgr subpathsAtPath:filePath];        NSInteger totalByteSize = 0;        for (NSString *subpath in subpaths) {            // 獲得全路徑            NSString *fullSubpath = [filePath stringByAppendingPathComponent:subpath];            // 判斷是否為文件            BOOL dir = NO;            [mgr fileExistsAtPath:fullSubpath isDirectory:&dir];            if (dir == NO) { // 文件                totalByteSize += [[mgr attributesOfItemAtPath:fullSubpath error:nil][NSFileSize] integerValue];            }        }        return totalByteSize;    } else { // 是一個文件        return [[mgr attributesOfItemAtPath:filePath error:nil][NSFileSize] integerValue];    }}調用傳入下面的路徑:NSFileManager *mgr = [NSFileManager defaultManager];// 緩存路徑NSString *caches = [NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES) lastObject];

5:文件操作(NSFileManager)iOS (轉)

iOS的沙盒機制,應用只能訪問自己應用目錄下的文件。iOS不像android,沒有SD 卡概念,不能直接訪問圖像、視頻等內容。iOS應用產生的內容,如圖像、文件、緩存內容等都必須存儲在自己的沙盒內。默認情況下,每個沙盒含有3個文件 夾:Documents, Library 和 tmp。Library包含Caches、PReferences目錄。Documents:蘋果建議將程序創建產生的文件以及應用瀏覽產生的文件數據保存在該目錄下,iTunes備份和恢復的時候會包括此目錄Library:存儲程序的默認設置或其它狀態信息;Library/Caches:存放緩存文件,保存應用的持久化數據,用于應用升級或者應用關閉后的數據保存,不會被itunes同步,所以為了減少同步的時間,可以考慮將一些比較大的文件而又不需要備份的文件放到這個目錄下。tmp:提供一個即時創建臨時文件的地方,但不需要持久化,在應用關閉后,該目錄下的數據將刪除,也可能系統在程序不運行的時候清除。a:獲取應用沙盒根路徑:-(void)dirHome{      NSString *dirHome=NSHomeDirectory();          NSLog(@"app_home: %@",dirHome);  } b:獲取Documents目錄路徑: -(NSString *)dirDoc{      //[NSHomeDirectory() stringByAppendingPathComponent:@"Documents"];      NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);      NSString *documentsDirectory = [paths objectAtIndex:0];      NSLog(@"app_home_doc: %@",documentsDirectory);      return documentsDirectory;  }  c:獲取Library目錄路徑:-(void)dirLib{      //[NSHomeDirectory() stringByAppendingPathComponent:@"Library"];      NSArray *paths = NSSearchPathForDirectoriesInDomains(NSLibraryDirectory, NSUserDomainMask, YES);      NSString *libraryDirectory = [paths objectAtIndex:0];      NSLog(@"app_home_lib: %@",libraryDirectory);  }  d:獲取Cache目錄路徑:-(void)dirCache{      NSArray *cacPath = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES);      NSString *cachePath = [cacPath objectAtIndex:0];      NSLog(@"app_home_lib_cache: %@",cachePath);  } e:獲取Tmp目錄路徑:-(void)dirTmp{      //[NSHomeDirectory() stringByAppendingPathComponent:@"tmp"];      NSString *tmpDirectory = NSTemporaryDirectory();      NSLog(@"app_home_tmp: %@",tmpDirectory);  }  f:創建文件夾:-(void *)createDir{      NSString *documentsPath =[self dirDoc];      NSFileManager *fileManager = [NSFileManager defaultManager];      NSString *testDirectory = [documentsPath stringByAppendingPathComponent:@"test"];      // 創建目錄      BOOL res=[fileManager createDirectoryAtPath:testDirectory withIntermediateDirectories:YES attributes:nil error:nil];      if (res) {          NSLog(@"文件夾創建成功");      }else          NSLog(@"文件夾創建失敗");   } g:創建文件-(void *)createFile{      NSString *documentsPath =[self dirDoc];      NSString *testDirectory = [documentsPath stringByAppendingPathComponent:@"test"];      NSFileManager *fileManager = [NSFileManager defaultManager];      NSString *testPath = [testDirectory stringByAppendingPathComponent:@"test.txt"];      BOOL res=[fileManager createFileAtPath:testPath contents:nil attributes:nil];      if (res) {          NSLog(@"文件創建成功: %@" ,testPath);      }else          NSLog(@"文件創建失敗");  } h:寫數據到文件:-(void)writeFile{      NSString *documentsPath =[self dirDoc];      NSString *testDirectory = [documentsPath stringByAppendingPathComponent:@"test"];      NSString *testPath = [testDirectory stringByAppendingPathComponent:@"test.txt"];      NSString *content=@"測試寫入內容!";      BOOL res=[content writeToFile:testPath atomically:YES encoding:NSUTF8StringEncoding error:nil];      if (res) {          NSLog(@"文件寫入成功");      }else          NSLog(@"文件寫入失敗");  }  i:讀文件數據:-(void)readFile{      NSString *documentsPath =[self dirDoc];      NSString *testDirectory = [documentsPath stringByAppendingPathComponent:@"test"];      NSString *testPath = [testDirectory stringByAppendingPathComponent:@"test.txt"];  //    NSData *data = [NSData dataWithContentsOfFile:testPath];  //    NSLog(@"文件讀取成功: %@",[[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding]);      NSString *content=[NSString stringWithContentsOfFile:testPath encoding:NSUTF8StringEncoding error:nil];      NSLog(@"文件讀取成功: %@",content);  }  j:文件屬性:-(void)fileAttriutes{      NSString *documentsPath =[self dirDoc];      NSString *testDirectory = [documentsPath stringByAppendingPathComponent:@"test"];      NSFileManager *fileManager = [NSFileManager defaultManager];      NSString *testPath = [testDirectory stringByAppendingPathComponent:@"test.txt"];      NSDictionary *fileAttributes = [fileManager attributesOfItemAtPath:testPath error:nil];         NSArray *keys;      id key, value;      keys = [fileAttributes allKeys];      int count = [keys count];      for (int i = 0; i < count; i++)      {          key = [keys objectAtIndex: i];          value = [fileAttributes objectForKey: key];          NSLog (@"Key: %@ for value: %@", key, value);      }  }  k:刪除文件:-(void)deleteFile{      NSString *documentsPath =[self dirDoc];      NSString *testDirectory = [documentsPath stringByAppendingPathComponent:@"test"];      NSFileManager *fileManager = [NSFileManager defaultManager];      NSString *testPath = [testDirectory stringByAppendingPathComponent:@"test.txt"];         BOOL res=[fileManager removeItemAtPath:testPath error:nil];      if (res) {          NSLog(@"文件刪除成功");      }else          NSLog(@"文件刪除失敗");         NSLog(@"文件是否存在: %@",[fileManager isExecutableFileAtPath:testPath]?@"YES":@"NO");  }  

 


發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 武川县| 东方市| 阿勒泰市| 闻喜县| 阿荣旗| 秀山| 前郭尔| 凤山市| 金乡县| 兴义市| 游戏| 天等县| 徐汇区| 滨州市| 镇康县| 永登县| 资中县| 桃江县| 丰顺县| 黎川县| 肇源县| 新沂市| 沈阳市| 云阳县| 乌鲁木齐市| 沅陵县| 大兴区| 晴隆县| 临夏市| 故城县| 德安县| 甘孜县| 吉木乃县| 中阳县| 延吉市| 海城市| 上杭县| 崇信县| 鲁山县| 永川市| 德钦县|