2011年6月13日 星期一

Stanford CS193P Note-02

id: a pointer to any kind of object.

◎ MVC 小記: @class 的使用
   假設有一個 Model: CalculatorBrain class
   1. 在 CalculatorViewController.h 中只需引入 class 名稱時:
      @class CalculatorBrain;
   2. 在 CalculatorViewController.m 中才真正會使用到此 class:
      #import "CalculatorBrain.h"

Class Methods
    1. ex:
       + (id)alloc;   
      // makes space for an object of the receiver's class (always pair w/init)

    2. Can not access instance variables inside

Instance Variables
    Scope: Instance Variables By default, instance variables are @protected
            (only the class and subclasses can access).

------------------------------------------------------------------------------------------------------

Properties
    1. Mark all of your instance variables @private.
    2. Use @property and "dot notation" to access instance variables.
       a. You can get the compiler to generate set/get method declarations with
          @property directive
       b. If you use the readonly keyword, only the getter will be declared
          @property (readonly) int eye;   
          // does not declare a setEye: method
    3. Create methods to set/get an instance variable's value
    4. Let the compiler help you with implementation using @synthesize!

    5. 建議的作法:
// Header (.h) file:
@interface MyObject : NSObject
{
@private
    int p_eye; // private variable
}
@property int eye;
@end

// Implementation (.m) file:
@implementation MyObject
@synthesize eye = p_eye;
/*
- (int)eye {
    return p_eye;
}
- (void)setEye:(int)anInt {
    p_eye = anInt;
}
*/
@end

    6. Notice:
       access to instance variable using property of self instead of directly.

------------------------------------------------------------------------------------------------------

Private Properties
    You can declare a "private interface" to your class inside your implementation file.
    Example (this is all in MyObject's.m file):

/*
This is the "magic" to declare your private stuff. You can put properties and methods here, but not more instance variables.
*/
@interface MyObject()
@property double myEyesOnly;
@end

@implementation MyObject
@synthesize eye, myEyesOnly;
@end

=> The property myEyesOnly can only be set/get via self.myEyesOnly
    since it is private.

------------------------------------------------------------------------------------------------------

Introspection
    1. All objects that inherit from NSObject know these methods:
       a. isKindOfClass: returns whether an object is that kind of class
                             (inheritance included)
       b. isMemberOfClass: returns whether an object is that kind of class
                                 (no inheritance)
       c. respondsToSelector: returns whether an object responds to a given method

    2. You get a Class by sending the class method class to a class :)
       ex:
if ([obj isKindOfClass:[NSString class]]) {
    NSString *s = [(NSString *)obj stringByAppendingString:@”xyzzy”];
}

    3. Method testing methods take a selector (SEL)
       Special @selector() directive turns the name of a method into a selector
       ex:
if ([obj respondsToSelector:@selector(shoot)]) {
    [obj shoot];
}

    4. SEL is the Objective-C "type" for a selector
SEL shootSelector = @selector(shoot);
SEL moveToSelector = @selector(moveTo:);

Target/action uses this, e.g.
[button addTarget:self action:@selector(digitPressed:)]

    5. If you have a SEL, you can ask an object to perform it
       Using the performSelector: or performSelector:withObject: methods in NSObject
       ex:
[obj performSelector:shootSelector];
[obj performSelector:moveToSelector withObject:coordinate];

------------------------------------------------------------------------------------------------------

nil
   1. The value of an object pointer that does not point to anything
   2. NSObject sets all its instance variables to zero
      Thus, instance variables that are pointers to objects start out with the value of nil.
   3. Can be implicitly tested in an if statement
      ex: if(obj){} //curly braces will execute if obj points to an object
   4. Sending messages to nil is (mostly) okay. No code gets executed.
      ex:
      a. If the method returns a value, it will return zero.
         int i = [obj methodWhichReturnsAnInt]; // i will be zero if obj is nil
      b. Be careful if the method returns a C struct. Return value is undefined.
         CGPoint p = [obj getLocation]; //p will have an undefined value if obj is nil

------------------------------------------------------------------------------------------------------

BOOL
    1. Objective-C's boolean "type" (actually just a typedef)
    2. YES means "true," NO means "false"
    3. NO == 0, YES is anything else

沒有留言:

張貼留言

注意:只有此網誌的成員可以留言。