首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式碼之家 >> 編程語言 >  >> C語言 >> 關於C語言

Objective-C 基礎知識總覽:啟動流程、語法、記憶體、常用類別

     編輯:關於C語言

App 啟動流程

  1. `main()` 建立 `UIApplication` 實例與委派
  2. 委派實作 `-application:didFinishLaunchingWithOptions:` 設定根 `UIViewController`
  3. 根控制器載入 View、添加子控制項、建立介面

基本語法

  • 訊息傳遞:`[receiver message]` (動態綁定)
  • 繼承:`@interface Child : Parent`
  • 實例建立:
    • 自動釋放:`NSString *s = [NSString string];`
    • 手動管理:`NSString *s = [[NSString alloc] init]; [s release];`
  • 類別結構:`.h` 宣告 (介面)、`.m` 實作。方法預設 `public`,實例變數預設 `@protected`。`+` 類別方法,`-` 實例方法。
  • Protocol:類似 Java `interface`,定義必要/可選方法。
  • 屬性宣告:
    
    @property (nonatomic, retain) NSString *name; // 非 ARC
    @property (nonatomic, strong) NSString *name; // ARC
    @synthesize name = _name; // 自動產生 getter/setter
    
  • 分類:擴充既有類別方法,無法加實例變數。
    
    @interface NSString (Utilities)
    - (BOOL)isURL;
    @end
    @implementation NSString (Utilities)
    - (BOOL)isURL { return [self hasPrefix:@"https://"]; }
    @end
    
  • IBOutlet / IBAction:Interface Builder 連結標記,編譯時忽略。
  • 異常處理:`@try/@catch/@finally` (需 Mac OS X 10.3+)。
  • id 型別:任意物件指標,動態派發,無需轉型。
  • 集合類別:
    • `NSArray` 不可變、`NSMutableArray` 可變
    • `NSDictionary` 鍵值對 (如 Java Map)
    • 快速枚舉:`for (id obj in array)`
    • 列舉器:`NSEnumerator *e = [array objectEnumerator]; while (obj = [e nextObject])`

記憶體管理核心 (非 ARC)

  • 擁有權規則:`alloc`/`new`/`copy`/`mutableCopy` → 你擁有 → 需 `release`/`autorelease`
  • 工廠方法回傳自動釋放物件
  • `dealloc` 中釋放持有物件,最後呼叫 `[super dealloc]`
  • ARC 環境下編譯器自動插入 `retain`/`release`,禁用手動呼叫

常用 Foundation 類別速查

類別用途關鍵方法
NSString不可變字串`stringWithFormat:` `isEqualToString:` `hasPrefix:` `componentsSeparatedByString:` `substringWithRange:` `writeToFile:atomically:`
NSMutableString可變字串`appendString:` `deleteCharactersInRange:`
NSArray不可變陣列`objectAtIndex:` `count` `indexOfObject:` `sortedArrayUsingSelector:`
NSMutableArray可變陣列`addObject:` `removeObjectAtIndex:` `insertObject:atIndex:`
NSDictionary不可變字典`objectForKey:` `allKeys` `allValues`
NSMutableDictionary可變字典`setObject:forKey:` `removeObjectForKey:`
NSFileManager檔案系統操作`fileExistsAtPath:` `createDirectoryAtPath:withIntermediateDirectories:` `enumeratorAtPath:`
NSFileHandle檔案讀寫`readDataToEndOfFile` `writeData:` `seekToFileOffset:`
NSData位元組緩衝區`dataWithBytes:length:` `bytes` `length` `writeToFile:atomically:`
NSNotificationCenter進程內廣播`addObserver:selector:name:object:` `postNotificationName:object:userInfo:`
NSTimer定時器`scheduledTimerWithTimeInterval:target:selector:userInfo:repeats:`
NSThread / NSOperationQueue多執行緒`detachNewThreadSelector:toTarget:withObject:` `addOperationWithBlock:`

檔案系統常用路徑


NSHomeDirectory()                    // App 沙盒根目錄
[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) firstObject] // Documents
[NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES) firstObject]   // Caches
[[NSBundle mainBundle] resourcePath] // App Bundle 資源目錄 (唯讀)
  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式碼之家 All Rights Reserved