所以我有下面的代碼,我想知道在objective-c中哪個被認爲是更好的「風格」。Objective-C造型
選項1:
id temp = [dictionary objectForKey: @"aBooleanValue"];
BOOL var = (temp) ? [ temp intValue ] : NO;
選項2:
BOOL var = ([dictionary objectForKey: @"aBooleanValue"]) ? [[dictionary objectForKey: @"aBooleanValue"] intValue ] : NO;
我相信性能也比較類似,因爲包含HashMap具有恆定的查找時間。如果不再使用temp變量,是否值得?
只是一個無關的註釋:如果可能的話,你應該使用'[whatever boolValue]',而不是'[不管intValue]'。 – 2011-03-04 16:06:28
有什麼問題:'BOOL var = [[dictionary objectForKey:@「aBooleanValue」] boolValue];'?? – Jasarien 2011-03-04 16:10:13
除了Jonathan Sterling:以及如果你想要的整數值。您應該使用integerValue並使用NSInteger而不是int和intValue。 – V1ru8 2011-03-04 16:10:56