Search code examples
iosobjective-cwatchkit

What does (NSDictionary<NSString *,id> * __nonnull) mean?


The parameter type is (NSDictionary<NSString *,id> * __nonnull), and it means the parameter should be a dictionary and never be null. Then what does <NSString *, id> means? Are 'NSString *' and 'id' protocol names? Does it mean that dictionary must be a json? But NSString are always object. Anyone help?


Solution

  • It is the syntax of Objective C light weight generics. This feature is mainly included for fixing Objective C - Swift interoperability issues.

    NSDictionary<NSString *,id> means, the dictionary keys should be of type NSString and it's value can be any type of object (id).

    For Example: If you want to declare a dictionary that will hold NSString values only, you can declare it like:

    NSDictionary<NSString *,NSString *> *stringDictionary;
    

    However in Objective C, you can set any-type of values to this dictionary, there will be no errors. But if you are using the same stringDictionary object from a swift class, you can only set a string value to this dictionary, otherwise it'll throw an error.

    Please check this article for more details.