2012年11月22日 星期四

NSError用法

之前我經常都會寫成這樣

 

NSError *fileError = nil;
....
if(fileError != nil)
....

 

後來才發現這樣是錯的!!

 

下面是從apple文件擷取出來的片段

Important: Success or failure is indicated by the return value of the method. Although Cocoa methods that indirectly return error objects in the Cocoa error domain are guaranteed to return such objects if the method indicates failure by directly returning nil or NO, you should always check that the return value is nil or NO before attempting to do anything with the NSError object.

 

大意是說,應該先檢查回傳的結果是否正確,再去用NSError。

所以應該寫成

 

NSString *fileContents;    
NSError *fileError;

fileContents = [NSString stringWithContentsOfFile:fileOnDisk
encoding:NSMacOSRomanStringEncoding
error:&fileError];

if(fileContents == nil) {
NSLog(@"Error : %@", [fileError localizedDescription]);
// ... i.e. handle the error here more
return ...; // often returning after handling the errors, sometimes you might continue
}

但是為什麼要先檢查結果呢?

看了這篇http://stackoverflow.com/questions/2069039/error-handling-with-nsurlconnection-sendsynchronousrequest之後

他舉例下載檔案有時候會在下載途中遭遇錯誤,並且被framework解決掉。雖然結果有正確回傳回來,但是NSError物件還是被設定了。

 

參考連結:http://stackoverflow.com/questions/1808929/handling-nserror-when-reading-from-file/1810301#1810301

2012年11月18日 星期日

Mango, House

最近突然愛上聽電子音樂,找了幾首Tiesto跟Armin Van Burren的歌來聽。

這兩位是屬於Trance風格的舞曲,聽起來的感覺是旋律性比較強,有的會有女性人聲搭配。

 

後來在Youtube上的推薦名單上亂點,無意間聽到了一個俄羅斯的DJ-Mango的作品

youtube播放清單

在假日慵懶的下午來杯咖啡,聽聽這多麼令人舒服的混音作品。

人生,活在當下。