ObjectiveC

Last-modified: 2012-05-28 (月) 00:28:47

ObjectiveCとは?

Cをオブジェクト指向したもの。

Compiler directive, コンパイラディレクティブ

ソースコード中で、「@」から始まる構文のこと。
「@property」は、プロパティを宣言する。
「@synthesize」は、アクセッサを自動的に生成させる。「@synthesize name = _name」でアクセッサnameをインスタンス変数_nameに対して作成する事を意味する。

他言語との構文比較。

クラス。

  • ObjectiveC
    @interface MyClass : NSObject
    {
      int member_value;
    }
    - (int)method:(int)value1:(int)value2:(float)value3; // "-"はインスタンスメソッド。引数はコロンで区切って書く。
    + (int)method:(int)value1;  // "+" はクラスメソッド
    @end
  • C++
    class MyClass : public NSObject
    {
    private:
      int member_value;
    public:
      int method(int value1, int value2, float value3);
      static int method(int value1);
    };
  • C
    struct MyClassData
    {
      int member_value;
    };
    struct MyClassAPI
    {
      int (*method)(int value1, int value2, float value3);
    };
    int MyClassStatic_method(int value1);
  • C#
    class MyClass : NSValue
    {
      private int member_value;
      public int method(int value1, int value2, float value3);
      public static int method(int value1);
    }

インタフェース

  • ObjectiveC
    @protocol MyInterface
    {
    }
    - (int)method:(int)value1:(int)value2:(float)value3;
    @end
  • C++
    class MyInterface
    {
    public:
      virtual int method(int value1, int value2, float value3)=0;
    };
  • C
    struct MyInterface
    {
      int (*method)(int value1, int value2, float value3);
    };
  • C#
    interface MyInterface
    {
      int method(int value1, int value2, float value3);
    }