I know NSDictionaries
as something where you need a key
in order to get a value
. But how can I iterate over all keys
and values
in a NSDictionary
, so that I know what keys there are, and what values there are? I know there is something called a for-in-loop in JavaScript
. Is there something similar in Objective-C
?
相关问题
- CALayer - backgroundColor flipped?
- Core Data lightweight migration crashes after App
- How can I implement password recovery in an iPhone
- State preservation and restoration strategies with
- “Zero out” sensitive String data in Swift
相关文章
- 现在使用swift开发ios应用好还是swift?
- UITableView dragging distance with UIRefreshContro
- TCC __TCCAccessRequest_block_invoke
- How to use doMC under Windows or alternative paral
- Where does a host app handle NSExtensionContext#co
- xcode 4 garbage collection removed?
- Unable to process app at this time due to a genera
- Swift - hide pickerView after value selected
This is iteration using block approach:
With autocompletion is very fast to set, and you do not have to worry about writing iteration envelope.
The block approach avoids running the lookup algorithm for every key:
Even though
NSDictionary
is implemented as a hashtable (which means that the cost of looking up an element isO(1)
), lookups still slow down your iteration by a constant factor.My measurements show that for a dictionary
d
of numbers ...... summing up the numbers with the block approach ...
... rather than the loop approach ...
... is about 40% faster.
EDIT: The new SDK (6.1+) appears to optimise loop iteration, so the loop approach is now about 20% faster than the block approach, at least for the simple case above.
Yes,
NSDictionary
supports fast enumeration. With Objective-C 2.0, you can do this:The alternate method (which you have to use if you're targeting Mac OS X pre-10.5, but you can still use on 10.5 and iPhone) is to use an
NSEnumerator
: