Objective-C Runtime bindings and wrapper for Rust.

Messaging objects

Objective-C objects can be messaged using the msg_send! macro:

rust let cls = Class::get("NSObject").unwrap(); let obj: *mut Object = msg_send![cls, new]; let hash: usize = msg_send![obj, hash]; let is_kind: BOOL = msg_send![obj, isKindOfClass:cls]; // Even void methods must have their return type annotated let _: () = msg_send![obj, release];

Declaring classes

Classes can be declared using the ClassDecl struct. Instance variables and methods can then be added before the class is ultimately registered.

The following example demonstrates declaring a class named MyNumber that has one ivar, a u32 named _number and a number method that returns it:

``` rust let superclass = Class::get("NSObject").unwrap(); let mut decl = ClassDecl::new(superclass, "MyNumber").unwrap();

// Add an instance variable decl.addivar::("number");

// Add an ObjC method for getting the number extern fn mynumberget(this: &Object, cmd: Sel) -> u32 { unsafe { *this.getivar("number") } } unsafe { decl.addmethod(sel!(number), mynumberget as extern fn(&Object, Sel) -> u32); }

decl.register(); ```

Exceptions

By default, if the msg_send! macro causes an exception to be thrown, this will unwind into Rust resulting in unsafe, undefined behavior. However, this crate has an "exception" feature which, when enabled, wraps each msg_send! in a @try/@catch and panics if an exception is caught, preventing Objective-C from unwinding into Rust.