だから私はこれをしたい:
switch (keyPath) {
case @"refreshCount":
//do stuff
case @"timesLaunched":
//do other stuff
}
ただし、スイッチの数量として使用できるのは整数のみであるようです。これを行う唯一の方法は、文字列を整数識別子に解析してから、switchステートメントを実行することですか?
このような:
nsinteger num = nil;
if (keyPath isEqual:@"refreshCount") {
num = 0
}
if (keyPath isEqual:@"timesLaunched") {
num = 1
}
頻繁に呼び出されるため、このコードをできるだけ速く最適化しようとしています。
ありがとう、
ニック
注:はい、KVOを使用しているので、「コールバック」で文字列を受信しています。
注2:最初にswitchステートメントを検討したのは、元のコードの実装は次のようなものでした。
if ([keyPath isEqual:@"refreshCount"] && ([[change valueForKey:@"new"] intValue] == 10)) { //
NSLog(@"achievemnt hit inside");
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"title" message:@"Achievement Unlocked!" delegate:self cancelButtonTitle:@"cancel" otherButtonTitles:nil];
[alert show];
異なるXX値を使用して、これらをすべて同じ方法で実行したいと思います。
if ([keyPath isEqual:@"refreshCount"] && ([[change valueForKey:@"new"] intValue] == 10)) {
//unlock small achievement
}
if ([keyPath isEqual:@"refreshCount"] && ([[change valueForKey:@"new"] intValue] == 50)) {
//unlock bigger achievement
}
//etc
これは私には非常に非効率に思えましたが、おそらく私は間違っています。