IOS開發基礎知識--片段16,ios基礎知識--片段

來源:互聯網
上載者:User

IOS開發基礎知識--片段16,ios基礎知識--片段

 

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");  }  

 

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.