#[macro_use]
extern crate objc;
use objc::{encode, Id, WeakId};
use objc::runtime::{Class, Object};
fn main() {
let cls = Class::get("NSObject").unwrap();
println!("NSObject size: {}", cls.instance_size());
println!("NSObject ivars:");
for ivar in cls.instance_variables().iter() {
println!("{}", ivar.name());
}
let obj: Id<Object> = unsafe {
let obj: *mut Object = msg_send![cls, alloc];
let obj: *mut Object = msg_send![obj, init];
Id::from_retained_ptr(obj)
};
println!("NSObject address: {:?}", &*obj as *const Object);
let isa: *const Class = unsafe {
*obj.get_ivar("isa")
};
println!("NSObject isa: {:?}", isa);
let hash_sel = sel!(hash);
let hash_method = cls.instance_method(hash_sel).unwrap();
let hash_return = hash_method.return_type();
println!("-[NSObject hash] return type: {}", &*hash_return);
assert!(encode::<usize>() == &*hash_return);
let hash: usize = unsafe {
msg_send![obj, hash]
};
println!("NSObject hash: {}", hash);
let obj = obj.share();
let weak = WeakId::new(&obj);
println!("Weak reference is nil? {}", weak.load().is_none());
println!("Releasing object");
drop(obj);
println!("Weak reference is nil? {}", weak.load().is_none());
}