火曜日, 10月 23, 2012

Objective-C のクラスメソッド

Objective-Cにも静的なメソッドというものがあります。

Objective-Cの用語ではクラスメソッドというものです。

Javaでいうところのstaticにあたります。

当然ながら、メンバ変数を持つことはありません。

メソッドの宣言に+(プラス記号)をつけます。

#import <stdio.h>
#import <Foundation/NSObject.h>

@interface Test:NSObject
{
  int index;
}
- (void) hello;
- (void) setIndex:(int) i;
+ (void) dump;
@end

@implementation Test
- (void) setIndex:(int) i
{
  self->index=i;
}
-(void)hello
{
  printf("Hello NSObject %d\n",self->index);
}
+(void)dump
{
  printf("Class method = static");
}
@end

int main()
{
  [Test dump];
  return 0;
} 

他にも@propertyや@synthesizeなどいうキーワードがありますが、GNUstepはサポートしていません。

参照先がreleaseされたときの挙動を変えるものです。
@property(nonatomic, assign)
@property(nonatomic, retain)

PHP: 定数を扱う

プロジェクトごとの定数を扱うクラス Config\Constants の紹介です。 <?php namespace Config; class Constants {     public const DB_USER = "linguist...