#![allow(unstable)]
#[macro_use]
extern crate objc;
use objc::{encode, Id, ToMessage, WeakId};
use objc::runtime::{Class, Object, Sel};
fn main() {
let cls = Class::get("NSObject").unwrap();
println!("NSObject size: {}", cls.instance_size());
println!("NSObject ivars:");
for ivar in cls.instance_variables().as_slice().iter() {
println!("{}", ivar.name());
}
let obj: Id<Object> = unsafe {
let obj = msg_send![cls, alloc];
let obj = msg_send![obj, init];
Id::from_retained_ptr(obj)
};
println!("NSObject address: {:?}", obj.as_ptr());
let isa: *const Class = unsafe {
*obj.get_ivar("isa")
};
println!("NSObject isa: {:?}", isa);
let hash_sel = Sel::register("hash");
let hash_method = cls.instance_method(hash_sel).unwrap();
let hash_return = hash_method.return_type();
println!("-[NSObject hash] return type: {}", hash_return.as_slice());
assert!(encode::<usize>() == hash_return.as_slice());
let hash = unsafe {
(msg_send![obj, hash]) as usize
};
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());
}