pub unsafe trait NSObjectProtocol {
// Provided methods
fn isEqual(&self, other: Option<&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: Option<&AnyObject>) -> bool
fn isEqual(&self, other: Option<&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 AnyObject::downcast_ref or Retained::downcast if your
intention is to use this to cast an object to another, and see
Apple’s documentation for more details on what you may
(and what you may not) do with this information in general.
Sourcefn is_kind_of<T: ClassType>(&self) -> bool
👎Deprecated: use isKindOfClass directly, or cast your objects with AnyObject::downcast_ref
fn is_kind_of<T: ClassType>(&self) -> bool
isKindOfClass directly, or cast your objects with AnyObject::downcast_refCheck 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.
If using this for availability checking, you might want to consider
using the available! macro instead, as it is often more
performant than this runtime check.
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_unchecked(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 implementation 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);