標籤:
從昨天開始準備從Objective-C Programming: The Big Nerd Ranch Guide這本書入手,從頭紮實地學習一下OC,順便提高一下英文閱讀能力。
今天的知識重點在於類方法和執行個體方法的區別:
NSDate *now = [NSDate date];
We say that the date method is a class method. That is, you cause the method to execute by sending a message to the NSDate class. The date method returns a pointer to an instance of NSDate.
date是一種類方法,你發送訊息的目標是NSDate類,該方法傳回值為一個NSDate的執行個體指標。
double seconds = [now timeIntervalSince1970];
We say that timeIntervalSince1970 is an instance method. You cause the method to execute by sending a message to an instance of the class. The timeIntervalSince1970 method returns a double.
timeIntervalSince1970是一種執行個體方法,你執行該方法時發送訊息的目標是類的一個執行個體,該方法傳回值為一個double型變數。
NSDate *later = [now dateByAddingTimeInterval:100000];
dateByAddingTimeInterval: is another instance method. This method takes one argument. You can determine this by the colon in the method name. This method also returns a pointer to an instance of NSDate.
dateByAddingTimeInterval是另一種執行個體變數。該方法需要一個參數。通過方法名中的冒號區分。該方法同樣返回一個指向NSDate執行個體的指標。
IOS類方法,執行個體方法