4 ms·
> How do you subclass an ObjC class from Rust — say, to create a delegate for a UI element? The objc::declare module includes ClassDecl, which enables register
by kattrali 10y ago
> How do you subclass an ObjC class from Rust — say, to create a delegate for a UI element?
The objc::declare module includes ClassDecl, which enables registering a new Objective-C class.
objc::declare documentation: http://sasheldon.com/rust-objc/objc/declare/index.html http://sasheldon.com/rust-objc/objc/declare/index.html
Example usage: https://github.com/kattrali/rust-mac-app-examples/blob/master/5-declaring-new-objc-class/src/main.rs#L56 https://github.com/kattrali/rust-mac-app-examples/blob/maste...
> How is type safety handled? It looks like msg_send itself isn't type safe, but there are hand-written bindings… is that right?
Correct. msg_send operates on objc::runtime::Object references. The handwritten bindings use the impl_objc_class macro referenced in the post to define separate types for different Objective-C classes, and make messaging the wrong type of object a compile-time rather than a runtime error.
The impl_objc_class macro implements the ObjCClass and PartialEq traits for a new type: https://github.com/kattrali/rust-mac-app-examples/blob/master/5-declaring-new-objc-class/src/objc_class.rs#L34 https://github.com/kattrali/rust-mac-app-examples/blob/maste...