[objective-c]教程五-----objective-c其它相關內容

改定履歷:

2012-05-07---------新建文本文檔


正文:

本文原文地址


1. 日誌:

objective-c日誌消息:

NSLog ( @"Logging message: %@", [myObject myMehtod] );

2. objective-c循環

用一個循環遍歷objective-c數組:

NSArray *myList = [NSArray arrayWithObjects:@"One", @"Two", @"Three", nil];
 
for (NSString *myData in myList) {
    NSLog(@"data: %@", myData);
}

3. objective-c線程

爲支持線程及異常處理,需打開如下編譯器開關:

-fobjc-exceptions

objecive-c中創建同步阻塞:
- (void)myMethod
{
    ...
    @synchronized(self) {
        ...
    }
    ...
}

※ 線程通過"@synchronized"關鍵字獲取鎖,將進入臨界區.

※ 其它線程將會被阻塞,直到該鎖被釋放.

4 objective-c異常處理:

拋出異常:

NSException *exception = [NSException exceptionWithName: @"MyException"
                                                 reason: @"The reason"
                                               userInfo: nil];
@throw exception;

※ Exception throwing and catching is resource intense in Objective-C(不好翻譯)

@try {
   ...
}
@catch (NSException *exception) {
   ...
}
@catch (id e) {
   // Catch any un-caught exception
   ...
}
@finally {
  // Will always execute regardless there is an exception or not
   ...
}


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章