#[repr(C)]pub struct Class { /* private fields */ }
Expand description
A type that represents an Objective-C class.
Implementations§
Source§impl Class
impl Class
Sourcepub fn get(name: &str) -> Option<&'static Class>
pub fn get(name: &str) -> Option<&'static Class>
Returns the class definition of a specified class, or None
if the
class is not registered with the Objective-C runtime.
Sourcepub fn classes() -> MallocBuffer<&'static Class>
pub fn classes() -> MallocBuffer<&'static Class>
Obtains the list of registered class definitions.
Sourcepub fn classes_count() -> usize
pub fn classes_count() -> usize
Returns the total number of registered classes.
Sourcepub fn superclass(&self) -> Option<&Class>
pub fn superclass(&self) -> Option<&Class>
Returns the superclass of self, or None
if self is a root class.
Sourcepub fn instance_size(&self) -> usize
pub fn instance_size(&self) -> usize
Returns the size of instances of self.
Examples found in repository?
8fn main() {
9 // Get a class
10 let cls = class!(NSObject);
11 println!("NSObject size: {}", cls.instance_size());
12
13 // Inspect its ivars
14 println!("NSObject ivars:");
15 for ivar in cls.instance_variables().iter() {
16 println!("{}", ivar.name());
17 }
18
19 // Allocate an instance
20 let obj = unsafe {
21 let obj: *mut Object = msg_send![cls, alloc];
22 let obj: *mut Object = msg_send![obj, init];
23 StrongPtr::new(obj)
24 };
25 println!("NSObject address: {:p}", obj);
26
27 // Access an ivar of the object
28 let isa: *const Class = unsafe {
29 *(**obj).get_ivar("isa")
30 };
31 println!("NSObject isa: {:?}", isa);
32
33 // Inspect a method of the class
34 let hash_sel = sel!(hash);
35 let hash_method = cls.instance_method(hash_sel).unwrap();
36 let hash_return = hash_method.return_type();
37 println!("-[NSObject hash] return type: {:?}", hash_return);
38 assert!(hash_return == usize::encode());
39
40 // Invoke a method on the object
41 let hash: usize = unsafe {
42 msg_send![*obj, hash]
43 };
44 println!("NSObject hash: {}", hash);
45}
Sourcepub fn instance_method(&self, sel: Sel) -> Option<&Method>
pub fn instance_method(&self, sel: Sel) -> Option<&Method>
Returns a specified instance method for self, or None
if self and
its superclasses do not contain an instance method with the
specified selector.
Examples found in repository?
8fn main() {
9 // Get a class
10 let cls = class!(NSObject);
11 println!("NSObject size: {}", cls.instance_size());
12
13 // Inspect its ivars
14 println!("NSObject ivars:");
15 for ivar in cls.instance_variables().iter() {
16 println!("{}", ivar.name());
17 }
18
19 // Allocate an instance
20 let obj = unsafe {
21 let obj: *mut Object = msg_send![cls, alloc];
22 let obj: *mut Object = msg_send![obj, init];
23 StrongPtr::new(obj)
24 };
25 println!("NSObject address: {:p}", obj);
26
27 // Access an ivar of the object
28 let isa: *const Class = unsafe {
29 *(**obj).get_ivar("isa")
30 };
31 println!("NSObject isa: {:?}", isa);
32
33 // Inspect a method of the class
34 let hash_sel = sel!(hash);
35 let hash_method = cls.instance_method(hash_sel).unwrap();
36 let hash_return = hash_method.return_type();
37 println!("-[NSObject hash] return type: {:?}", hash_return);
38 assert!(hash_return == usize::encode());
39
40 // Invoke a method on the object
41 let hash: usize = unsafe {
42 msg_send![*obj, hash]
43 };
44 println!("NSObject hash: {}", hash);
45}
Sourcepub fn instance_variable(&self, name: &str) -> Option<&Ivar>
pub fn instance_variable(&self, name: &str) -> Option<&Ivar>
Returns the ivar for a specified instance variable of self, or None
if self has no ivar with the given name.
Sourcepub fn instance_methods(&self) -> MallocBuffer<&Method>
pub fn instance_methods(&self) -> MallocBuffer<&Method>
Describes the instance methods implemented by self.
Sourcepub fn conforms_to(&self, proto: &Protocol) -> bool
pub fn conforms_to(&self, proto: &Protocol) -> bool
Checks whether this class conforms to the specified protocol.
Sourcepub fn adopted_protocols(&self) -> MallocBuffer<&Protocol>
pub fn adopted_protocols(&self) -> MallocBuffer<&Protocol>
Get a list of the protocols to which this class conforms.
Sourcepub fn instance_variables(&self) -> MallocBuffer<&Ivar>
pub fn instance_variables(&self) -> MallocBuffer<&Ivar>
Describes the instance variables declared by self.
Examples found in repository?
8fn main() {
9 // Get a class
10 let cls = class!(NSObject);
11 println!("NSObject size: {}", cls.instance_size());
12
13 // Inspect its ivars
14 println!("NSObject ivars:");
15 for ivar in cls.instance_variables().iter() {
16 println!("{}", ivar.name());
17 }
18
19 // Allocate an instance
20 let obj = unsafe {
21 let obj: *mut Object = msg_send![cls, alloc];
22 let obj: *mut Object = msg_send![obj, init];
23 StrongPtr::new(obj)
24 };
25 println!("NSObject address: {:p}", obj);
26
27 // Access an ivar of the object
28 let isa: *const Class = unsafe {
29 *(**obj).get_ivar("isa")
30 };
31 println!("NSObject isa: {:?}", isa);
32
33 // Inspect a method of the class
34 let hash_sel = sel!(hash);
35 let hash_method = cls.instance_method(hash_sel).unwrap();
36 let hash_return = hash_method.return_type();
37 println!("-[NSObject hash] return type: {:?}", hash_return);
38 assert!(hash_return == usize::encode());
39
40 // Invoke a method on the object
41 let hash: usize = unsafe {
42 msg_send![*obj, hash]
43 };
44 println!("NSObject hash: {}", hash);
45}