Objective-C Syntax
Objective-C 特殊語法整理,方便日後忘記回憶
Contents
Class interface
@interface ClassName:Parent <protocol..>
{
@protected
//variables
@public
//variables
@private
//variables
}
// methods
@end
Methods in interface section
// None Argument
- (return_type) method_Name;
// One Argument
- (return_type) method_Name: (arg1_type) arg;
// Many argument
- (return_type) method_Name: (arg1_type) arg1
arg2_name: (arg2_type) arg2
arg3_name: (arg3_type) arg3;
// 省略Argument name:
- (return_type) method_name: (arg1_type) arg1 :(arg2_type) arg2;
Property in interface section
//@property type variable;
@property int v;
//相同type的property 宣告
@property int a,b,c,d,e;
Class Implement section
@implementation ClassName
// property implement
@synthesize pt;
// ... method implement ...
@end
Get class object
[obj class];
try block
@try{
// try block
}
@catch (NSException *e)
{
//catch block
//throw same exception
@throw;
}
// optional
@finally
{
// finally block
}
Category
@interface class_name ( category_name )
// 不需要{ }
- (xxx) xxx: (yyy) zzz;
// ...
@end
@implementation class_name ( category_name )
//implement methods
@end
Protocol Declare
@protocol protocol_name
// -(ret) method:(type) arg;
@end
id adopted protocol
id < protocal_name > obj_name;
selector
SEL action;
action = @selector( method_name );
// call obj.action(..)
[obj performSelector: action];
//....
if ([obj respondsToSelector: @selector( setx:sety )] == YES )
//...
沒有留言:
張貼留言