Module runtime

Source
Expand description

§Direct runtime bindings.

This module contains safe(r) bindings to common parts of the Objective-C runtime. See the ffi module for details on the raw bindings.

§Example

Using features of the runtime to query information about NSObject.

use objc2::runtime::{AnyClass, NSObject};
use objc2::{sel, ClassType, Encode};

fn main() {
    // Get the class representing `NSObject`
    let cls = NSObject::class();

    // Inspect various properties of the class
    println!("NSObject superclass: {:?}", cls.superclass());
    println!("NSObject size: {}", cls.instance_size());
    println!(
        "-[NSObject alloc] would work: {}",
        cls.responds_to(sel!(alloc))
    );
    println!(
        "+[NSObject alloc] would work: {}",
        cls.metaclass().responds_to(sel!(alloc))
    );

    // Inspect an instance variable on the class
    //
    // Note: You should not rely on the `isa` ivar being available,
    // this is only for demonstration.
    let ivar = cls
        .instance_variable(c"isa")
        .expect("no ivar with name 'isa' found on NSObject");
    println!(
        "Instance variable {:?} has type encoding {:?}",
        ivar.name(),
        ivar.type_encoding()
    );
    assert!(<*const AnyClass>::ENCODING.equivalent_to_str(ivar.type_encoding().to_str().unwrap()));

    // Inspect a method of the class
    let method = cls.instance_method(sel!(hash)).unwrap();
    println!(
        "-[NSObject hash] takes {} parameters",
        method.arguments_count()
    );
    let hash_return = method.return_type();
    println!("-[NSObject hash] return type: {hash_return:?}");
    assert!(usize::ENCODING.equivalent_to_str(hash_return.to_str().unwrap()));

    // Create an instance
    let obj = NSObject::new();

    println!("NSObject address: {obj:p}");

    // Read an ivar on the object
    let isa: *const AnyClass = unsafe { *ivar.load(&obj) };
    println!("NSObject isa: {isa:?}");
}

Re-exports§

pub use crate::ffi::BOOL;Deprecated
pub use crate::ffi::NO;Deprecated
pub use crate::ffi::YES;Deprecated

Structs§

AnyClass
An opaque type that represents an Objective-C class.
AnyObject
An Objective-C object.
AnyProtocol
An opaque type that represents a protocol in the Objective-C runtime.
Bool
The Objective-C BOOL type.
ClassBuilder
A type for creating a new class and adding new methods and ivars to it before registering it.
Ivar
An opaque type that represents an instance variable.
Method
A type that represents a method in a class definition.
NSObject
The root class of most Objective-C class hierarchies.
NSZone
A type used to identify and manage memory zones.
ProtocolBuilder
A type for creating a new protocol and adding new methods to it before registering it.
ProtocolObject
An object representing any object that implements a specified protocol.
Sel
A method selector.
VerificationError
Failed verifying selector on a class.

Traits§

ImplementedBy
An internal helper trait for ProtocolObject.
MessageReceiver
Types that can directly be used as the receiver of Objective-C messages.
MethodImplementation
Types that can be used as the implementation of an Objective-C method.
NSObjectProtocol
The methods that are fundamental to most Objective-C objects.

Type Aliases§

ClassDeprecated
Use AnyClass instead.
Imp
A pointer to the start of a method implementation.
ObjectDeprecated
Use AnyObject instead.
ProtocolDeprecated
Use AnyProtocol instead.