私は現在、ユーザーがいくつかのオブジェクトをセットアップするために使用している NSDictionnary でいくつかのパラメーターを定義するプロジェクトに取り組んでいます。たとえば、パラメーター param1=xxx、param2=yyy、gain=3.5 で Sound オブジェクトを作成するように要求できます。次に、パラメーター speed=10、active=YES、name=zzz で Enemi オブジェクトを作成します。
{
active = NO;
looping = YES;
soundList = "FINAL_PSS_imoverhere_all";
speed = 100.0;
}
次に、クラスをインスタンス化し、この辞書から ivar を自動的に設定したいと考えています。このパラメーターが存在することを確認するコードを実際にいくつか書きましたが、特にパラメーターがオブジェクトではない場合 (float または bool)、実際にパラメーター値を設定するのに問題があります。
これが私がこれまでにやっていることです:
//aKey is the name of the ivar
for (NSString *aKey in [properties allKeys]){
//create the name of the setter function from the key (parameter -> setParameter)
NSString *setterName = [aKey stringByReplacingCharactersInRange:NSMakeRange(0,1) withString:[[aKey substringToIndex:1] uppercaseString]];
setterName = [NSString stringWithFormat:@"set%@:",setterName];
SEL setterSelector = NSSelectorFromString(setterName);
//Check if the parameter exists
if ([pge_object respondsToSelector:setterSelector]){
//TODO : automatically set the parameter
}
else{
[[PSMessagesChecker sharedInstance]logMessage:[NSString stringWithFormat:@"Cannot find %@ on %@", aKey, [dict objectForKey:@"type"]] inColor:@"red"];
NSLog(@"Cannot find %@ on %@", aKey, [dict objectForKey:@"type"]);
}
}
}
ご覧のとおり、パラメーターがオブジェクトに存在することがわかったら、どうすればよいかわかりません。「performSelector... withObject...」を使用しようとしましたが、一部のパラメーターが非オブジェクト (float または bool) であることが問題です。また、setter を使用して、パラメーターのクラスを取得しようとしましたが、役に立ちませんでした。
誰かがそのようなことをすることができましたか?