Search code examples
objective-ccocoarustnsdictionary

Rust Cocoa - How to make an NSDictionary?


I've been reading around trying to understand it. You can see here dictionaryWithObjects:objects the takes an array of Objects and Keys:

NSDictionary *dictionary = [NSDictionary dictionaryWithObjects:objects
                                                       forKeys:keys
                                                         count:count];

https://developer.apple.com/documentation/foundation/nsdictionary#overview


but initWithObjectsAndKeys_ only a single Object for input? 🤷🏻‍♂️

unsafe fn initWithObjectsAndKeys_(self, firstObject: *mut Object) -> *mut Object

https://docs.rs/cocoa/0.24.0/cocoa/foundation/trait.NSDictionary.html#tymethod.initWithObjectsAndKeys_


Solution

  • Many of Rust cocoa APIs are direct wrappers of the underlying Objective-C APIs, like initWithObjectsAndKeys: https://developer.apple.com/documentation/foundation/nsdictionary/1574190-initwithobjectsandkeys?language=objc. Callers are expected to pass a pointer to the first element of a null-terminated array containing alternating value and key elements for the dictionary.