2012-02-02 3 views
0

私はRPN計算機を作ることに関わる作業に取り組んでいます。私はダイナミックなプロジェクト内の別の場所からの操作で追加できるようにする機能のレジストリシステムの種類を実装する方が良いだろうと信じてクラスメソッド、状態を保存する、関数を登録するためのより良い方法

+ (NSSet *) noOpOperations { 
    return [NSSet setWithObjects:@"π", nil]; 
} 

+ (NSSet *) unaryOperations { 
    return [NSSet setWithObjects:@"sin",@"cos",@"log",@"+/-", nil]; 
} 

+ (NSSet *) binaryOperations { 
    return [NSSet setWithObjects:@"+",@"-",@"*",@"/", nil]; 
} 
+ (NSSet *) operations { 
    /* surely there is a better way - is it possible to save this call and reuse it? Or what about having the objects register themselves so you can add operations more easily? */ 
    return [[self noOpOperations] setByAddingObjectsFromSet: 
      [[self unaryOperations] setByAddingObjectsFromSet: 
      [self binaryOperations]]]; 
} 

+ (BOOL) isOperation:operand { 
    return [[self operations] containsObject:operand]; 
} 

:私は現在、文字列は以下のように操作されるかどうかを確認するには、クラスのメソッドを使用していますしかし、私はそれがクラス変数を必要とすると思います。私は今それをやっているよりもこれを行う良い方法はありますか?このような状況のため

+0

'class variable'はどういう意味ですか? .mファイルには、__attribute __((コンストラクタ))関数を使用して初期化する静的変数が1つあります。 –

+0

これはまさに私が探していたものです - ありがとう! –

答えて

1

私の個人的なソリューション:

#define INT_OBJ(x) [NSNumber numberWithInt:x] 

@implementation MyClass 

static NSDictionary *operations; 

enum { 
    kOperationNoOp = 1, 
    kOperationUnaryOp, 
    kOperationBinaryOp, 
}; 

+(void) initialize 
{ 
    operations = [NSDictionary dictionaryWithObjectsAndKeys:@"π", INT_OBJ(kOperationNoOp), 

                  // unary operations 
                  @"sin", INT_OBJ(kOperationUnaryOp), 
                  @"cos", INT_OBJ(kOperationUnaryOp), 
                  @"log", INT_OBJ(kOperationUnaryOp), 
                  @"+/-", INT_OBJ(kOperationUnaryOp), 

                  // binary operations 
                  @"+", INT_OBJ(kOperationBinaryOp), 
                  @"-", INT_OBJ(kOperationBinaryOp), 
                  @"*", INT_OBJ(kOperationBinaryOp), 
                  @"/", INT_OBJ(kOperationBinaryOp), nil]; 
} 

-(BOOL) isNoOpOperation:(NSString *) arg 
{ 
    return [[operations objectForKey:arg] intValue] == kOperationNoOp; 
} 

-(BOOL) isUnaryOperation:(NSString *) arg 
{ 
    return [[operations objectForKey:arg] intValue] == kOperationUnaryOp; 
} 

-(BOOL) isBinaryOperation:(NSString *) arg 
{ 
    return [[operations objectForKey:arg] intValue] == kOperationBinaryOp; 
} 

-(BOOL) isAnOperation:(NSString *) arg 
{ 
    // if objectForKey: returns nil, intValue will return 0, telling us that the input is not an operation 
    return [[operations objectForKey:arg] intValue] != 0; 
} 

@end 

私はそれが非常に簡単かつ拡張が容易であることがわかりました。

+0

私はあなたが複数の行に渡ってdictionaryWithObjectsAndKeysをどのように壊したか好きです。私のプロジェクトでXcodeを使ってみると、エラーが発生しました。読みやすくするために別の行で呼び出しを中断するのは合法ですか? –

+0

Nevermind - #defineの後にエラーの原因となったセミコロンがありました。とにかくありがとう! –

+0

@JoshuaAresty問題なく、助けてくれるだけで、答えの横のチェックマークをクリックして回答を受け入れることを忘れないでください! –

関連する問題