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
- "Normal" methods you are used to
- Can access instance variables inside as if they were locals
- Can send message to self and super inside
If a superclass of yours call a method on self, it will your implementation ( if one exists)
- Example calling syntax:
Class Methods ( 類方法 )
=> A group of global function that are collected for this class.
- Starts with a plus. Used for allocation, singletons, utilities
+ ( 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
- Example calling syntax ( a little different from instance methods )
Ship *theMothership = [Ship motherShip];
Ship *newShip = [Ship shipWithTurrentCount:5];
int turrestsOnMeiumSizedShip = [Ship turretsOnShipOfSize:4];
Instance Variables (實例變數)
- Scope
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
{
@private
int eye;
}
- (int) eye;
-(void) setEye:(int)anInt;
@end
- Now anyone can access your instance variable using "dot notation"
int eyeValue = someObject.eye; // get the instance variable's current value
- @property
@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
- An @property does not have to match an instance variable name
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!
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
@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
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;
}
}
好累... 先寫到這裡!