lukaskollmer / objc

🔮 NodeJS ↔ Objective-C bridge (experimental)
MIT License
98 stars 20 forks source link

Clarification of Object->NSDictionary Conversion #7

Closed jmbldwn closed 6 years ago

jmbldwn commented 6 years ago

I have a property I need to set with a dictionary. The obj.ns() method says it turns objects into dictionaries, but NSDictionary keys can be more than just strings. For example to configure video output settings, in Objective C I do this:

videoDataOutput.videoSettings = @{
    (id)kCVPixelBufferPixelFormatTypeKey: [NSNumber numberWithUnsignedInteger:kCVPixelFormatType_32BGRA]
};

I can't quite figure out how to construct the JS object to pass to obj.ns() to make this happen. Am I missing something?

lukaskollmer commented 6 years ago

According to the ECMA spec,

Properties are identified using key values. A property key value is either an ECMAScript String value or a Symbol value

objc.ns therefore will always create a NSDictionary w/ NSString keys. If you want some custom id as a key, you'll have to create a NSMutableDictionary and fill it via setObject:forKey:

jmbldwn commented 6 years ago

Thanks. This works. It might be nice someday to have a smoother way to pull in .h files that define constants that the runtime expects, including conversion from 4BCC codes to numbers. It ends up being a little clunky to have to do this manually.