2010年12月18日 星期六

Object C - Class 3

- Objective C
Methods ( Class and Instance )
Instance Variables
Properties
Dynamic Binding
Introspection
nil and BOOL

- Foundation Framework ( 框架 )
NSObject
NSString
NSMutableString
NSNumber
NSValue
NSData
NSDate
NSArray
NSDictionary
NSSet
Enumeration
Property Lists

Method Syntax

- (NSArray *)shipsAtPoint:((CGPoint) bombLocation withDamage: (BOOL)damaged;

"-" : Dash for instance method ( 減號是實作的方法 )
"+": Plus for class method (加號是類別的方法 )

"(NSArray *)": Return type in parentheses ( 方法返回值的類型 )
=>ex. (void) 不回傳值

"shipsAtPoint:" : First part of method name , Full name is shipsAtPoint:withDamage:
"withDamage:" : second part of method name

"(CGPoint)": Type of first argument in parentheses. This one happens to be a C struct ( 第一個參數類型恰為一個C的結構體)-CGpoint 表視為螢幕上的一點

"(BOOL)": Type of second argument in parentheses.

"bombLocation":Name of first argument. Use it like a local variable inside method definition. 參數名字可以為任何數但不能為空

"damaged": Name of second argument.

Line up colons when there are lots of arguments (or argument names are long).

-(void)splitViewController : (UISplitViewController*)

svc

willHideViewController : (UIViewController *)aViewController

withBarButtonItem : (UIBarButtonItem *)barButtonItem

forPopoverController : (UIPopoverController *)popoverController ;

(":" 排成一排比較好讀 )


Use IBAction (same as void) to alert Interface Builder of an action.

- (IBAction)digitPressed : (UIButton *) sender;

- (IBAction)digitPressed : (id)sender;

- (IBAction)digitPressed : sender; // same as (id)sender version

- (IBAction)digitPressed ;



Instance Methods ( 實作方法 )
  • Start with a dash
-(void *) setGoodman;
  • "Normal" methods you are used to
  • Can access instance variables inside as if they were locals
  • Can send message to self and super inside
Both dispatch the message to the colling object, but use different implementation.
If a superclass of yours call a method on self, it will your implementation ( if one exists)
  • Example calling syntax:
BOOL destroyed = [ ship dropBomb:bombType at:dropPoint from;height];

Class Methods ( 類方法 )
=> A group of global function that are collected for this class.
  • Starts with a plus. Used for allocation, singletons, utilities
+ ( id ) alloc; // 分配記憶體 makes space for an object of the receiver's class (always pair w/init)
+ ( id ) motherShip; // 單一的母類別 return the one and only, shared (singleton) mother ship instance
+ ( int ) turretsOnShipOfSize: ( int ) shipSize ; // information utility method
  • Can not access instance variables inside ( 不能使用實例變數 )
  • Messages to self and super mean something a little different
Both invoke only class method. Inheritance does work.
  • Example calling syntax ( a little different from instance methods )
CalculatorBrain *brain = [[CalculatorBrain alloc] init];
Ship *theMothership = [Ship motherShip];
Ship *newShip = [Ship shipWithTurrentCount:5];
int turrestsOnMeiumSizedShip = [Ship turretsOnShipOfSize:4];

Instance Variables (實例變數)
  • Scope
By default, instance variables are @protected (only the class and subclasses can access).
Can Be marked @private (only the class can access) or @public (anyone can access).

Properties
  • Create methods to set/get an instance variable's value
@interface MyObject : NSObject
{
@private
int eye;
}
- (int) eye;
-(void) setEye:(int)anInt;
@end

  • Now anyone can access your instance variable using "dot notation"
someobject.eye = newEyeValue; // set the instance variable
int eyeValue = someObject.eye; // get the instance variable's current value


  • @property
You can get the comiler to generate set/get method declaration with @property directive

@interface MyObject : NSObject
{
@private
int eye;
}
@property int eye; // 相當於 - (int) eye; -(void) setEye:(int)anInt;
@end

  • If you use the readonly keyword, only the getter will be declared
@property ( readonly ) int eye; // does not declare a setEye: method ( 這將使他無法創建setter )

  • An @property does not have to match an instance variable name
@Property 不需要與實例變數名相同
For example...
@interface MyObject : NSObject
{
@private
int p_eye;
}
@property int eye;
@end
  • In fact, you do not even have to have a matching variable at all
事實上,你根本就不用有個變數
The following is perfectly legal
@interface MyObject : NSObject
{
}
@property int eye;
@end

  • But whatever you declare, you must then implement
不管你如何宣告,你就必須實作它
For example, consider the following header (.h) file:
考慮這個類別方法
@interface MyObject : NSObject
{
@private
int eye;
}
@property int eye;
@end
The corresponding implementation (.m) file might look like this:
對應的實作方法
@implementation MyObject
- (int) eye {
return eye;
}
- (void)setEye: (int) anInt {
eye = anInt;
}
@end

  • Or consider the case where the variable name is different
若考慮不同的變數名稱
For example..
Header (.h) file:
@interface MyObject : NSObject
{
@private
int p_eye;
}
@property int eye;
@end
對比的實作
Corresponding implementation (.m) file:
@implementation MyObject
- (int) eye {
return p_eye;
}
- (void)setEye: (int) anInt {
p_eye = anInt;
}
@end

  • Or how about the "no corresponding variable" case?
若是這種沒有回變數的例子
Header (.h) file:
@interface MyObject : NSObject
{
}
@property (readonly) int eye;
@end
Implementation (.m) file:
@implementation MyObject
- (int)eye
{
return ;
}
@end

  • Let the compiler help you with implementation using @synthesize!
Synthesize 綜合性地處理

Header (.h) file:
@interface MyObject : NSObject
{
@private
int eye;
}
@property int eye;
@end
Implementation (.m) file:
@implementation MyObject
@synthesize eye;
/*
- (int) eye{
return eye;
}
-(void)setEye:(int)anInt {
eye = anInt;
}
@end

  • You can even get @synthesize to use a different variable
@synthesize eye = p_eye;

  • If you use @synthesize, you can still implement one or the other
@implementation MyObject
@synthesize eye;
- (void)setEye:(int)anInt {
if (anInt > 0) eye = anInt; //從寫這段讓eye 為正值, 若為負值則不動作
}
@end
The method -(int)eye will still be implemented for you by @synthesize
Your implementation of setEye: is the one that will count
If you implemented both the setter and the getter, the @synthesize would be ignored

  • It's common to use dot notation to access ivars inside your class
使用self.xxx 來存取變數
example... if eye is an instance variable ...

int x = eye;

.... inside a method is not the same as ...
int x = self.eye;
The latter calls the getter method ( which is usually what you want for subclassability).
這表示他呼叫一種方法來存取變數,這樣比較好
  • But occasionally things can go terribly wrong! ( 但使用上要小心)
what's wrong with the following code?
-(void) setEye:(int) anInt
{
self.eye = anInt;
}
Infinite loop. (造成無窮回圈)
Can happen with the getter too..
-(int) eye
{
if (self.eye > 0)
{
return eye;
} else
{
return -1;
}
}

好累... 先寫到這裡!



iphone stanford class link

- Iphone Stanford Class link

- Objective -C 中文字幕

2010年12月16日 星期四

Object C

A good explain for Object C

http://hinoto424.blogspot.com/2010/11/iphone-objective-c-01.html

類別 (Class):"類別是物件的藍圖。"
物件 (Object):"物件,是類別產生出來的實體 (Instance)。"
方法 (Method):Objective-C 裡面,方法分成兩種;
Class Method (類別方法),顧名思義,這個方法是作用在類別上面的,表示方法如下:
+ (void) someClassMethod;
+ 號,代表這是一個 Class Method() 裡面是著名使用這個方法之後,回傳值的種類,(void) 就表示這個方法沒有回傳值。someClassMethod 則是這個方法的名字。

當呼叫 Class Method 的時候,不需要事先產生 Instance / Object (實體 / 物件)

另一種就叫 Instance Method (實體方法),也就是說,這個方法是作用在實體,也就是你的物件上面,表示方法如下:

- (void) someInstanceMethod;

Class Method 的差別,在於
- 號,代表這是一個 Instance Method
其他則與 Class Method 相同。

另外,要使用 Instance Method必須先產生 Instance / Object (實體 / 物件)

訊息傳遞 (Message):