版權聲明

所有的部落格文章都可以在右邊[blog文章原始檔案]下載最原始的文字檔案,並依你高興使用 docutil 工具轉換成任何對應的格式方便離線閱覽,除了集結成書販賣歡迎任意取用,引用

Objective-C Syntax

Objective-C Syntax

Objective-C 特殊語法整理,方便日後忘記回憶

Import

#import 'xxxxx.h'

@class

// 類似extern,讓compiler了解symbol型態,再沒有使用method時可用
@class class_name

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 )
//...

沒有留言:

Related Posts with Thumbnails