3 ms·
Fast NSDictionary traversal in Objective-C
- mayoff 15y agoAnother approach: @interface NSDictionary (objectForKeyList) - (id)objectForKeyList:(id)key, ...; @end @implementation NSDictionary (objectForKeyList) - (id)objectForKeyList:(id)key, ... { id object = self; va_list ap; va_start(ap, key); for ( ; key; key = va_arg(ap, id)) object = [object objectForKey:key]; va_end(ap); return object; } @end
- terhechte 15y agoAlso a good idea, thanks for sharing. This could even be expanded to check whether list items are integers and call objectAtIndex: instead in order to traverse Array/Dictionary mixups.
- jcizzle 15y agoEh. Not really seeing it. I think the category approach is much better (cleaner, easier documented, conventional) and there really isn't a speed increase in using the preprocessor (anything that looks like a difference in time is due to either caching or CPU load at the time).