Posted on

上課筆記(二) – Objective-C語言介紹、物件存取

Objective-C語言

  1. @代表物件
    [@”Hello world”] => 方括弧是存取物件裡的方法
    例:

    NSString * str =@"World";
    str = [NSString stringWithFormat:@"Hello"];
    NSLog(@"str is: %@",str);
  2. Class:類別
    Object:物件及實體,包括
    (a) 名字(Name)
    (b) 屬性(State):field, attribute, member, 屬性, 狀態, 成員
    (c) 行為(Behavior):method, action, member function
    Message:使用方來傳遞訊息。例:NSString的StringWithString
    螢幕快照 2013-08-02 下午5.49.59
    PS: 若在#import <Foundation/Fundation.h>下面加上@Class Car;,代表把Car當作class,在.m檔才import。
  3. self(存取自己),只能用在implementation的『{』至『@end』之間。
  4. variable宣告:
    NSString * name = [[NSString alloc] init];
  5. 宣告時,前方的+或-代表是否要透過init alloc來回傳物件。
    +號是不用透過init alloc傳回物件
  6. 要公開(public)的放在.h,不公開的放在.m
  7. NSStrung * name = [[NSString alloc] init];
    等同於 NSString * name = [NSString new];
    =>配置記憶體與初始化是產生物件後的第一個動作
  8. Obj C可以使用id去動態新增method,所有obj c的語言都會被編譯成c語言。

Message Syntax

  1. 『物件.xxx』所存取的不是屬性,而是方法。是存取物件裡面的getter和setter。
    label.text = @"Description"; //等於 [label setText:@"Description"];
    NSString *str = label.text; //等於 NSString*str = [label text];
  2. 自動產生getter和setter
    @property int age; //會自動幫我們產生getter和setter
    @property (strong) NSString * name; //如果是物件,前面要加strong或weak
  3. 如果interface和implementation在同個檔案,在interface裡用@property,在implementation裡需加上@synthesize
    @synthesize age;//自動產生的變數名稱會是_age

    若分開為.h及.m檔,則.h檔宣告後,.m檔裡不用寫就會自動產生。

  4. 動態型別 – id:任何繼承NS Object的物件型別,宣告方式如下
    id someObject; //不用加*號 ( <s>id * someObj</s> )

    用法例子:在apple在view事件觸發時要接收事件,會希望任何class都可以去接收該事件都可以。
    當不知道是誰接收時,就會用id去宣告

  5. 空指標nil,物件型別的空值,等同於NULL。若將nil丟給方法則會像石沉大海一樣什麼都不發生也不會有error。=>經指正會產生空值error
  6. Obj C的BOOL是傳YES(true)及NO(false)。
  7. 要判斷現在的class裡是否有某個函數要用Selector,回傳的型別是SEL。使用範例如下
    -(void)methodWithNoArguments;
    SEL noArgumentSelector = @selector(methodWithNoArguments);
    
    -(void)methodWithOneArgument:(id)argument;
    SEL oneArgumentSelector = @selector(methodWithOneArgument:); // notice the colon here
    
    -(void)methodWIthTwoArguments:(id)argumentOne and:(id)argumentTwo;
    SEL twoArgumentSelector = @selector(methodWithTwoArguments:and:); // notice the argument names are omitted

    Selectors are generally passed to delegate methods and to callbacks to specify which method should be called on a specific object during a callback. For instance, when you create a timer, the callback method is specifically defined as:

    -(void)someMethod:(NSTimer*)timer;

    So when you schedule the timer you would use @selector to specify which method on your object will actually be responsible for the callback:

    @implementation MyObject
    
    -(void)myTimerCallback:(NSTimer*)timer
    {
        // do some computations
        if( timerShouldEnd ) {
          [timer invalidate];
        }
    }
    
    @end
    
    // ...
    
    int main(int argc, const char **argv)
    {
        // do setup stuff
        MyObject* obj = [[MyObject alloc] init];
        SEL mySelector = @selector(myTimerCallback:);
        [NSTimer scheduleTimerWithTimeInterval:30.0 target:obj selector:mySelector userInfo:nil repeats:YES];
        // do some tear-down
        return 0;
    }

    In this case you are specifying that the object obj be messaged with myTimerCallback every 30 seconds.
    假如一個function在該做的事完成後要執行selector callback,可以類似下面這樣寫

    -(void) someMethod:(id)handler selector:(SEL)selector {
        // some codes here...
    
        // execute callback function
        if( handler != nil &amp;&amp; selector != nil &amp;&amp; [handler respondsToSelector:selector] ) {
            [handler performSelector:selector];
        }
    }

    來源:http://stackoverflow.com/questions/297680/how-do-sel-and-selector-work-in-iphone-sdk

  8. 判斷class是否有該函數使用
    if([obj respondsToSelector:action]) { ... }