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§
sourcefn isEqual(&self, other: &AnyObject) -> bool
fn isEqual(&self, other: &AnyObject) -> bool
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.
sourcefn hash(&self) -> NSUInteger
fn hash(&self) -> NSUInteger
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.
sourcefn isKindOfClass(&self, cls: &AnyClass) -> bool
fn isKindOfClass(&self, cls: &AnyClass) -> bool
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.
sourcefn is_kind_of<T: ClassType>(&self) -> bool
fn is_kind_of<T: ClassType>(&self) -> bool
Check if the object is an instance of the class type, or one of its subclasses.
See isKindOfClass
for details.
sourcefn isMemberOfClass(&self, cls: &AnyClass) -> bool
fn isMemberOfClass(&self, cls: &AnyClass) -> bool
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.
sourcefn respondsToSelector(&self, aSelector: Sel) -> bool
fn respondsToSelector(&self, aSelector: Sel) -> bool
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() }
};
sourcefn conformsToProtocol(&self, aProtocol: &AnyProtocol) -> bool
fn conformsToProtocol(&self, aProtocol: &AnyProtocol) -> bool
Check whether the object conforms to a given protocol.
See Apple’s documentation for details.
sourcefn description(&self) -> Retained<NSObject> ⓘ
fn description(&self) -> Retained<NSObject> ⓘ
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:?}");
sourcefn debugDescription(&self) -> Retained<NSObject> ⓘ
fn debugDescription(&self) -> Retained<NSObject> ⓘ
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.
sourcefn isProxy(&self) -> bool
fn isProxy(&self) -> bool
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());
sourcefn retainCount(&self) -> NSUInteger
fn retainCount(&self) -> NSUInteger
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);