For an NSArray
, Objective-C allows us to specify the type of the values to be stored in the array. For example, I can declare an array of NSString
's as follows:
NSArray<NSString*> *arrayOfStrings;
While it is not necessarily enforced by the compiler and runtime environment, such an indication can be handy both for readability and slightly simpler code syntax.
I was wondering if such a syntax existed for NSDictionary
. For example, to specify the value type in the dictionary, does something like the following exist:
NSDictionary<UIImage*> *imageMappings;
where the dictionary's values are specified to be of type UIImage
.
Additional questions:
Can key types be specified for NSDictionary
?
If possible, how can I specify value type without key type?
Are there other common data structures in Foundation
that have this generic parametrization?
For NSDictionary
, you specify the type for both key and value:
NSDictionary<NSString*, UIImage*> *imageMappings
If you don't want a specific type for the key, use id<NSCopying>
or even NSObject *
.
Only NSArray
and NSDictionary
support this notation.