Trait objc2::runtime::NSObjectProtocol

source ·
pub unsafe trait NSObjectProtocol {
    // Provided methods
    fn isEqual(&self, other: &AnyObject) -> bool
       where Self: Sized + Message { ... }
    fn hash(&self) -> NSUInteger
       where Self: Sized + Message { ... }
    fn isKindOfClass(&self, cls: &AnyClass) -> bool
       where Self: Sized + Message { ... }
    fn is_kind_of<T: ClassType>(&self) -> bool
       where Self: Sized + Message { ... }
    fn isMemberOfClass(&self, cls: &AnyClass) -> bool
       where Self: Sized + Message { ... }
    fn respondsToSelector(&self, aSelector: Sel) -> bool
       where Self: Sized + Message { ... }
    fn conformsToProtocol(&self, aProtocol: &AnyProtocol) -> bool
       where Self: Sized + Message { ... }
    fn description(&self) -> Retained<NSObject> 
       where Self: Sized + Message { ... }
    fn debugDescription(&self) -> Retained<NSObject> 
       where Self: Sized + Message { ... }
    fn isProxy(&self) -> bool
       where Self: Sized + Message { ... }
    fn retainCount(&self) -> NSUInteger
       where Self: Sized + Message { ... }
}
Expand description

The methods that are fundamental to most Objective-C objects.

This represents the NSObject protocol.

You should rarely need to use this for anything other than as a trait bound in extern_protocol!, to allow your protocol to implement Debug Hash, PartialEq and Eq.

This trait is exported under objc2_foundation::NSObjectProtocol, you probably want to use that path instead.

§Safety

Like with other protocols, the type must represent a class that implements the NSObject protocol.

Provided Methods§

source

fn isEqual(&self, other: &AnyObject) -> bool
where Self: Sized + Message,

Check whether the object is equal to an arbitrary other object.

Most objects that implement NSObjectProtocol also implements the PartialEq trait. If the objects you are comparing are of the same type, you likely want to use that instead.

See Apple’s documentation for details.

source

fn hash(&self) -> NSUInteger
where Self: Sized + Message,

An integer that can be used as a table address in a hash table structure.

Most objects that implement NSObjectProtocol also implements the Hash trait, you likely want to use that instead.

See Apple’s documentation for details.

source

fn isKindOfClass(&self, cls: &AnyClass) -> bool
where Self: Sized + Message,

Check if the object is an instance of the class, or one of its subclasses.

See Apple’s documentation for more details on what you may (and what you may not) do with this information.

source

fn is_kind_of<T: ClassType>(&self) -> bool
where Self: Sized + Message,

Check if the object is an instance of the class type, or one of its subclasses.

See isKindOfClass for details.

source

fn isMemberOfClass(&self, cls: &AnyClass) -> bool
where Self: Sized + Message,

Check if the object is an instance of a specific class, without checking subclasses.

Note that this is rarely what you want, the specific class of an object is considered a private implementation detail. Use isKindOfClass instead to check whether an object is an instance of a given class.

See Apple’s documentation for more details.

source

fn respondsToSelector(&self, aSelector: Sel) -> bool
where Self: Sized + Message,

Check whether the object implements or inherits a method with the given selector.

See Apple’s documentation for more details.

§Example

Check whether NSApplication has the effectiveAppearance method before calling it, to support systems older than macOS 10.14 where the method was added.

use objc2_app_kit::{NSApplication, NSAppearance, NSAppearanceNameAqua};
use objc2::runtime::NSObjectProtocol;
use objc2::sel;

let appearance = if obj.respondsToSelector(sel!(effectiveAppearance)) {
    NSApplication::sharedApplication(mtm).effectiveAppearance()
} else {
    unsafe { NSAppearance::appearanceNamed(NSAppearanceNameAqua).unwrap() }
};
source

fn conformsToProtocol(&self, aProtocol: &AnyProtocol) -> bool
where Self: Sized + Message,

Check whether the object conforms to a given protocol.

See Apple’s documentation for details.

source

fn description(&self) -> Retained<NSObject>
where Self: Sized + Message,

A textual representation of the object.

The returned class is NSString, but since that is defined in objc2-foundation, and NSObjectProtocol is defined in objc2, the declared return type is unfortunately restricted to be NSObject. It is always safe to cast the return value of this to NSString.

You might want to use the Debug impl of the object instead, or if the object implements Display, the to_string method.

§Example
use objc2::rc::Retained;
use objc2_foundation::{NSObject, NSObjectProtocol, NSString};

// SAFETY: Descriptions are always `NSString`.
let desc: Retained<NSString> = unsafe { Retained::cast(obj.description()) };
println!("{desc:?}");
source

fn debugDescription(&self) -> Retained<NSObject>
where Self: Sized + Message,

A textual representation of the object to use when debugging.

Like with description, the return type of this is always NSString.

LLVM’s po command uses this property to create a textual representation of the object. The default implemention returns the same value as description. Override either to provide custom object descriptions.

source

fn isProxy(&self) -> bool
where Self: Sized + Message,

Check whether the receiver is a subclass of the NSProxy root class instead of the usual NSObject.

See Apple’s documentation for details.

§Example
use objc2::runtime::{NSObject, NSObjectProtocol};

let obj = NSObject::new();
assert!(!obj.isProxy());
source

fn retainCount(&self) -> NSUInteger
where Self: Sized + Message,

The reference count of the object.

This can rarely be useful when debugging memory management issues, though beware that in most real-world scenarios, your object may be retained by several autorelease pools, especially when debug assertions are enabled, so this value may not represent what you’d expect.

§Example
use objc2::ClassType;
use objc2::runtime::{NSObject, NSObjectProtocol};

let obj = NSObject::new();
assert_eq!(obj.retainCount(), 1);
let obj2 = obj.clone();
assert_eq!(obj.retainCount(), 2);
drop(obj2);
assert_eq!(obj.retainCount(), 1);

Trait Implementations§

source§

impl ProtocolType for dyn NSObjectProtocol

source§

const NAME: &'static str = "NSObject"

The name of the Objective-C protocol that this type represents.
source§

fn protocol() -> Option<&'static AnyProtocol>

Get a reference to the Objective-C protocol object that this type represents. Read more
source§

impl<T> ImplementedBy<T> for dyn NSObjectProtocol

source§

impl<T> ImplementedBy<T> for dyn NSObjectProtocol + Send

source§

impl<T> ImplementedBy<T> for dyn NSObjectProtocol + Send + Sync

source§

impl<T> ImplementedBy<T> for dyn NSObjectProtocol + Sync

Implementors§