pub struct NSMutableDictionary<KeyType: ?Sized = AnyObject, ObjectType: ?Sized = AnyObject> { /* private fields */ }
NSDictionary
only.Expand description
************** Mutable Dictionary ***************
See also Apple’s documentation
Implementations§
Source§impl<KeyType: Message, ObjectType: Message> NSMutableDictionary<KeyType, ObjectType>
Convenience creation methods.
impl<KeyType: Message, ObjectType: Message> NSMutableDictionary<KeyType, ObjectType>
Convenience creation methods.
pub fn from_slices<CopiedKey>( keys: &[&CopiedKey], objects: &[&ObjectType], ) -> Retained<Self>
NSObject
only.pub fn from_retained_objects<CopiedKey>( keys: &[&CopiedKey], objects: &[Retained<ObjectType>], ) -> Retained<Self>
NSObject
only.Source§impl<KeyType: Message, ObjectType: Message> NSMutableDictionary<KeyType, ObjectType>
Convenience mutation methods.
impl<KeyType: Message, ObjectType: Message> NSMutableDictionary<KeyType, ObjectType>
Convenience mutation methods.
Sourcepub fn insert<CopiedKey>(&self, key: &CopiedKey, object: &ObjectType)
Available on crate feature NSObject
only.
pub fn insert<CopiedKey>(&self, key: &CopiedKey, object: &ObjectType)
NSObject
only.Inserts a key-value pair into the dictionary.
If the dictionary did not have this key present, the value is inserted. If the dictionary already had this key present, the value and the key is updated.
§Examples
use objc2_foundation::{ns_string, NSMutableDictionary, NSObject};
let dict = NSMutableDictionary::new();
dict.insert(ns_string!("key"), &*NSObject::new());
Source§impl<KeyType: Message, ObjectType: Message> NSMutableDictionary<KeyType, ObjectType>
Methods declared on superclass NSDictionary
.
impl<KeyType: Message, ObjectType: Message> NSMutableDictionary<KeyType, ObjectType>
Methods declared on superclass NSDictionary
.
NSDictionaryCreation.
pub fn dictionary() -> Retained<Self>
Sourcepub unsafe fn dictionaryWithObject_forKey(
object: &ObjectType,
key: &ProtocolObject<dyn NSCopying>,
) -> Retained<Self>
Available on crate feature NSObject
only.
pub unsafe fn dictionaryWithObject_forKey( object: &ObjectType, key: &ProtocolObject<dyn NSCopying>, ) -> Retained<Self>
NSObject
only.§Safety
key
should be of the correct type.
Sourcepub unsafe fn dictionaryWithObjects_forKeys_count(
objects: *mut NonNull<ObjectType>,
keys: *mut NonNull<ProtocolObject<dyn NSCopying>>,
cnt: NSUInteger,
) -> Retained<Self>
Available on crate feature NSObject
only.
pub unsafe fn dictionaryWithObjects_forKeys_count( objects: *mut NonNull<ObjectType>, keys: *mut NonNull<ProtocolObject<dyn NSCopying>>, cnt: NSUInteger, ) -> Retained<Self>
NSObject
only.§Safety
objects
must be a valid pointer or null.keys
must be a valid pointer or null.
pub fn dictionaryWithDictionary( dict: &NSDictionary<KeyType, ObjectType>, ) -> Retained<Self>
Sourcepub unsafe fn dictionaryWithObjects_forKeys(
objects: &NSArray<ObjectType>,
keys: &NSArray<ProtocolObject<dyn NSCopying>>,
) -> Retained<Self>
Available on crate features NSArray
and NSObject
only.
pub unsafe fn dictionaryWithObjects_forKeys( objects: &NSArray<ObjectType>, keys: &NSArray<ProtocolObject<dyn NSCopying>>, ) -> Retained<Self>
NSArray
and NSObject
only.§Safety
keys
generic should be of the correct type.
pub fn initWithDictionary( this: Allocated<Self>, other_dictionary: &NSDictionary<KeyType, ObjectType>, ) -> Retained<Self>
pub unsafe fn initWithDictionary_copyItems( this: Allocated<Self>, other_dictionary: &NSDictionary<KeyType, ObjectType>, flag: bool, ) -> Retained<Self>
Sourcepub unsafe fn initWithObjects_forKeys(
this: Allocated<Self>,
objects: &NSArray<ObjectType>,
keys: &NSArray<ProtocolObject<dyn NSCopying>>,
) -> Retained<Self>
Available on crate features NSArray
and NSObject
only.
pub unsafe fn initWithObjects_forKeys( this: Allocated<Self>, objects: &NSArray<ObjectType>, keys: &NSArray<ProtocolObject<dyn NSCopying>>, ) -> Retained<Self>
NSArray
and NSObject
only.§Safety
keys
generic should be of the correct type.
Source§impl<KeyType: ?Sized + Message, ObjectType: ?Sized + Message> NSMutableDictionary<KeyType, ObjectType>
impl<KeyType: ?Sized + Message, ObjectType: ?Sized + Message> NSMutableDictionary<KeyType, ObjectType>
Sourcepub unsafe fn cast_unchecked<NewKeyType: ?Sized + Message, NewObjectType: ?Sized + Message>(
&self,
) -> &NSMutableDictionary<NewKeyType, NewObjectType>
pub unsafe fn cast_unchecked<NewKeyType: ?Sized + Message, NewObjectType: ?Sized + Message>( &self, ) -> &NSMutableDictionary<NewKeyType, NewObjectType>
Unchecked conversion of the generic parameters.
§Safety
The generics must be valid to reinterpret as the given types.
Source§impl<KeyType: Message, ObjectType: Message> NSMutableDictionary<KeyType, ObjectType>
impl<KeyType: Message, ObjectType: Message> NSMutableDictionary<KeyType, ObjectType>
pub fn removeObjectForKey(&self, a_key: &KeyType)
Sourcepub unsafe fn setObject_forKey(
&self,
an_object: &ObjectType,
a_key: &ProtocolObject<dyn NSCopying>,
)
Available on crate feature NSObject
only.
pub unsafe fn setObject_forKey( &self, an_object: &ObjectType, a_key: &ProtocolObject<dyn NSCopying>, )
NSObject
only.§Safety
a_key
should be of the correct type.
pub fn init(this: Allocated<Self>) -> Retained<Self>
pub fn initWithCapacity( this: Allocated<Self>, num_items: NSUInteger, ) -> Retained<Self>
Source§impl<KeyType: Message, ObjectType: Message> NSMutableDictionary<KeyType, ObjectType>
Methods declared on superclass NSDictionary
.
impl<KeyType: Message, ObjectType: Message> NSMutableDictionary<KeyType, ObjectType>
Methods declared on superclass NSDictionary
.
Sourcepub unsafe fn initWithObjects_forKeys_count(
this: Allocated<Self>,
objects: *mut NonNull<ObjectType>,
keys: *mut NonNull<ProtocolObject<dyn NSCopying>>,
cnt: NSUInteger,
) -> Retained<Self>
Available on crate feature NSObject
only.
pub unsafe fn initWithObjects_forKeys_count( this: Allocated<Self>, objects: *mut NonNull<ObjectType>, keys: *mut NonNull<ProtocolObject<dyn NSCopying>>, cnt: NSUInteger, ) -> Retained<Self>
NSObject
only.§Safety
objects
must be a valid pointer or null.keys
must be a valid pointer or null.
Source§impl<KeyType: Message, ObjectType: Message> NSMutableDictionary<KeyType, ObjectType>
Methods declared on superclass NSObject
.
impl<KeyType: Message, ObjectType: Message> NSMutableDictionary<KeyType, ObjectType>
Methods declared on superclass NSObject
.
Source§impl<KeyType: Message, ObjectType: Message> NSMutableDictionary<KeyType, ObjectType>
NSExtendedMutableDictionary.
impl<KeyType: Message, ObjectType: Message> NSMutableDictionary<KeyType, ObjectType>
NSExtendedMutableDictionary.
pub fn addEntriesFromDictionary( &self, other_dictionary: &NSDictionary<KeyType, ObjectType>, )
pub fn removeAllObjects(&self)
pub fn removeObjectsForKeys(&self, key_array: &NSArray<KeyType>)
NSArray
only.pub fn setDictionary( &self, other_dictionary: &NSDictionary<KeyType, ObjectType>, )
Sourcepub unsafe fn setObject_forKeyedSubscript(
&self,
obj: Option<&ObjectType>,
key: &ProtocolObject<dyn NSCopying>,
)
Available on crate feature NSObject
only.
pub unsafe fn setObject_forKeyedSubscript( &self, obj: Option<&ObjectType>, key: &ProtocolObject<dyn NSCopying>, )
NSObject
only.§Safety
key
should be of the correct type.
Source§impl<KeyType: Message, ObjectType: Message> NSMutableDictionary<KeyType, ObjectType>
NSMutableDictionaryCreation.
impl<KeyType: Message, ObjectType: Message> NSMutableDictionary<KeyType, ObjectType>
NSMutableDictionaryCreation.
pub fn dictionaryWithCapacity(num_items: NSUInteger) -> Retained<Self>
pub unsafe fn dictionaryWithContentsOfFile( path: &NSString, ) -> Option<Retained<NSMutableDictionary<KeyType, ObjectType>>>
NSString
only.pub unsafe fn dictionaryWithContentsOfURL( url: &NSURL, ) -> Option<Retained<NSMutableDictionary<KeyType, ObjectType>>>
NSURL
only.pub unsafe fn initWithContentsOfFile( this: Allocated<Self>, path: &NSString, ) -> Option<Retained<NSMutableDictionary<KeyType, ObjectType>>>
NSString
only.pub unsafe fn initWithContentsOfURL( this: Allocated<Self>, url: &NSURL, ) -> Option<Retained<NSMutableDictionary<KeyType, ObjectType>>>
NSURL
only.Source§impl<KeyType: Message, ObjectType: Message> NSMutableDictionary<KeyType, ObjectType>
NSSharedKeySetDictionary.
impl<KeyType: Message, ObjectType: Message> NSMutableDictionary<KeyType, ObjectType>
NSSharedKeySetDictionary.
§Safety
keyset
should be of the correct type.
Source§impl<KeyType: Message, ObjectType: Message> NSMutableDictionary<KeyType, ObjectType>
NSKeyValueCoding.
impl<KeyType: Message, ObjectType: Message> NSMutableDictionary<KeyType, ObjectType>
NSKeyValueCoding.
pub unsafe fn setValue_forKey(&self, value: Option<&ObjectType>, key: &NSString)
NSKeyValueCoding
and NSString
only.Methods from Deref<Target = NSDictionary<KeyType, ObjectType>>§
Sourcepub unsafe fn objectForKey_unchecked(
&self,
key: &KeyType,
) -> Option<&ObjectType>
pub unsafe fn objectForKey_unchecked( &self, key: &KeyType, ) -> Option<&ObjectType>
Get a direct reference to the object corresponding to the key.
Consider using the objectForKey
method
instead, unless you’re seeing performance issues from the retaining.
§Safety
The dictionary must not be mutated while the reference is live.
Sourcepub unsafe fn to_vecs_unchecked(&self) -> (Vec<&KeyType>, Vec<&ObjectType>)
Available on crate feature alloc
only.
pub unsafe fn to_vecs_unchecked(&self) -> (Vec<&KeyType>, Vec<&ObjectType>)
alloc
only.Sourcepub unsafe fn keys_unchecked(&self) -> KeysUnchecked<'_, KeyType, ObjectType> ⓘ
Available on crate feature NSEnumerator
only.
pub unsafe fn keys_unchecked(&self) -> KeysUnchecked<'_, KeyType, ObjectType> ⓘ
NSEnumerator
only.Sourcepub unsafe fn objects_unchecked(
&self,
) -> ObjectsUnchecked<'_, KeyType, ObjectType> ⓘ
Available on crate feature NSEnumerator
only.
pub unsafe fn objects_unchecked( &self, ) -> ObjectsUnchecked<'_, KeyType, ObjectType> ⓘ
NSEnumerator
only.Sourcepub fn to_vecs(&self) -> (Vec<Retained<KeyType>>, Vec<Retained<ObjectType>>)
Available on crate feature alloc
only.
pub fn to_vecs(&self) -> (Vec<Retained<KeyType>>, Vec<Retained<ObjectType>>)
alloc
only.Two vectors containing respectively the dictionary’s keys and objects.
§Example
Iterate over the keys and values of the dictionary.
use objc2_foundation::{NSDictionary, ns_string};
let dict = NSDictionary::from_slices(
&[ns_string!("a"), ns_string!("b")],
&[ns_string!("a"), ns_string!("b")],
);
let (keys, objects) = dict.to_vecs();
for (key, obj) in keys.into_iter().zip(objects) {
assert_eq!(key, obj);
}
Sourcepub fn keys(&self) -> Keys<'_, KeyType, ObjectType> ⓘ
Available on crate feature NSEnumerator
only.
pub fn keys(&self) -> Keys<'_, KeyType, ObjectType> ⓘ
NSEnumerator
only.Iterate over the dictionary’s keys.
Sourcepub fn objects(&self) -> Objects<'_, KeyType, ObjectType> ⓘ
Available on crate feature NSEnumerator
only.
pub fn objects(&self) -> Objects<'_, KeyType, ObjectType> ⓘ
NSEnumerator
only.Iterate over the dictionary’s objects / values.
§Examples
use objc2_foundation::{ns_string, NSMutableDictionary, NSString};
let dict = NSMutableDictionary::new();
dict.insert(ns_string!("key1"), ns_string!("value1"));
dict.insert(ns_string!("key2"), ns_string!("value2"));
for obj in dict.objects() {
assert!(obj.hasPrefix(ns_string!("value")));
}
Sourcepub unsafe fn cast_unchecked<NewKeyType: ?Sized + Message, NewObjectType: ?Sized + Message>(
&self,
) -> &NSDictionary<NewKeyType, NewObjectType>
pub unsafe fn cast_unchecked<NewKeyType: ?Sized + Message, NewObjectType: ?Sized + Message>( &self, ) -> &NSDictionary<NewKeyType, NewObjectType>
Unchecked conversion of the generic parameters.
§Safety
The generics must be valid to reinterpret as the given types.
pub fn count(&self) -> NSUInteger
pub fn objectForKey(&self, a_key: &KeyType) -> Option<Retained<ObjectType>>
Sourcepub unsafe fn keyEnumerator(&self) -> Retained<NSEnumerator<KeyType>>
Available on crate feature NSEnumerator
only.
pub unsafe fn keyEnumerator(&self) -> Retained<NSEnumerator<KeyType>>
NSEnumerator
only.§Safety
The returned enumerator’s underlying collection should not be mutated while in use.
pub fn allKeys(&self) -> Retained<NSArray<KeyType>>
NSArray
only.pub fn allKeysForObject( &self, an_object: &ObjectType, ) -> Retained<NSArray<KeyType>>
NSArray
only.pub fn allValues(&self) -> Retained<NSArray<ObjectType>>
NSArray
only.pub fn description(&self) -> Retained<NSString>
NSString
only.pub fn descriptionInStringsFileFormat(&self) -> Retained<NSString>
NSString
only.Sourcepub unsafe fn descriptionWithLocale(
&self,
locale: Option<&AnyObject>,
) -> Retained<NSString>
Available on crate feature NSString
only.
pub unsafe fn descriptionWithLocale( &self, locale: Option<&AnyObject>, ) -> Retained<NSString>
NSString
only.§Safety
locale
should be of the correct type.
Sourcepub unsafe fn descriptionWithLocale_indent(
&self,
locale: Option<&AnyObject>,
level: NSUInteger,
) -> Retained<NSString>
Available on crate feature NSString
only.
pub unsafe fn descriptionWithLocale_indent( &self, locale: Option<&AnyObject>, level: NSUInteger, ) -> Retained<NSString>
NSString
only.§Safety
locale
should be of the correct type.
pub fn isEqualToDictionary( &self, other_dictionary: &NSDictionary<KeyType, ObjectType>, ) -> bool
Sourcepub unsafe fn objectEnumerator(&self) -> Retained<NSEnumerator<ObjectType>>
Available on crate feature NSEnumerator
only.
pub unsafe fn objectEnumerator(&self) -> Retained<NSEnumerator<ObjectType>>
NSEnumerator
only.§Safety
The returned enumerator’s underlying collection should not be mutated while in use.
pub fn objectsForKeys_notFoundMarker( &self, keys: &NSArray<KeyType>, marker: &ObjectType, ) -> Retained<NSArray<ObjectType>>
NSArray
only.pub unsafe fn writeToURL_error( &self, url: &NSURL, ) -> Result<(), Retained<NSError>>
NSError
and NSURL
only.Sourcepub unsafe fn keysSortedByValueUsingSelector(
&self,
comparator: Sel,
) -> Retained<NSArray<KeyType>>
Available on crate feature NSArray
only.
pub unsafe fn keysSortedByValueUsingSelector( &self, comparator: Sel, ) -> Retained<NSArray<KeyType>>
NSArray
only.§Safety
comparator
must be a valid selector.
Sourcepub unsafe fn getObjects_andKeys_count(
&self,
objects: *mut NonNull<ObjectType>,
keys: *mut NonNull<KeyType>,
count: NSUInteger,
)
pub unsafe fn getObjects_andKeys_count( &self, objects: *mut NonNull<ObjectType>, keys: *mut NonNull<KeyType>, count: NSUInteger, )
§Safety
objects
must be a valid pointer or null.keys
must be a valid pointer or null.
pub fn objectForKeyedSubscript( &self, key: &KeyType, ) -> Option<Retained<ObjectType>>
pub fn enumerateKeysAndObjectsUsingBlock( &self, block: &DynBlock<dyn Fn(NonNull<KeyType>, NonNull<ObjectType>, NonNull<Bool>) + '_>, )
block2
only.pub fn enumerateKeysAndObjectsWithOptions_usingBlock( &self, opts: NSEnumerationOptions, block: &DynBlock<dyn Fn(NonNull<KeyType>, NonNull<ObjectType>, NonNull<Bool>) + '_>, )
NSObjCRuntime
and block2
only.Sourcepub unsafe fn keysSortedByValueUsingComparator(
&self,
cmptr: NSComparator,
) -> Retained<NSArray<KeyType>>
Available on crate features NSArray
and NSObjCRuntime
and block2
only.
pub unsafe fn keysSortedByValueUsingComparator( &self, cmptr: NSComparator, ) -> Retained<NSArray<KeyType>>
NSArray
and NSObjCRuntime
and block2
only.§Safety
cmptr
must be a valid pointer.
Sourcepub unsafe fn keysSortedByValueWithOptions_usingComparator(
&self,
opts: NSSortOptions,
cmptr: NSComparator,
) -> Retained<NSArray<KeyType>>
Available on crate features NSArray
and NSObjCRuntime
and block2
only.
pub unsafe fn keysSortedByValueWithOptions_usingComparator( &self, opts: NSSortOptions, cmptr: NSComparator, ) -> Retained<NSArray<KeyType>>
NSArray
and NSObjCRuntime
and block2
only.§Safety
cmptr
must be a valid pointer.
pub fn keysOfEntriesPassingTest( &self, predicate: &DynBlock<dyn Fn(NonNull<KeyType>, NonNull<ObjectType>, NonNull<Bool>) -> Bool + '_>, ) -> Retained<NSSet<KeyType>>
NSSet
and block2
only.pub fn keysOfEntriesWithOptions_passingTest( &self, opts: NSEnumerationOptions, predicate: &DynBlock<dyn Fn(NonNull<KeyType>, NonNull<ObjectType>, NonNull<Bool>) -> Bool + '_>, ) -> Retained<NSSet<KeyType>>
NSObjCRuntime
and NSSet
and block2
only.Sourcepub unsafe fn getObjects_andKeys(
&self,
objects: *mut NonNull<ObjectType>,
keys: *mut NonNull<KeyType>,
)
👎Deprecated: Use -getObjects:andKeys:count: instead
pub unsafe fn getObjects_andKeys( &self, objects: *mut NonNull<ObjectType>, keys: *mut NonNull<KeyType>, )
This method is unsafe because it could potentially cause buffer overruns. You should use -getObjects:andKeys:count:
§Safety
objects
must be a valid pointer or null.keys
must be a valid pointer or null.
pub unsafe fn writeToFile_atomically( &self, path: &NSString, use_auxiliary_file: bool, ) -> bool
NSString
only.pub unsafe fn writeToURL_atomically( &self, url: &NSURL, atomically: bool, ) -> bool
NSURL
only.pub fn fileSize(&self) -> c_ulonglong
NSFileManager
only.pub fn fileModificationDate(&self) -> Option<Retained<NSDate>>
NSFileManager
and NSDate
only.pub fn fileType(&self) -> Option<Retained<NSString>>
NSFileManager
and NSString
only.pub fn filePosixPermissions(&self) -> NSUInteger
NSFileManager
only.pub fn fileOwnerAccountName(&self) -> Option<Retained<NSString>>
NSFileManager
and NSString
only.pub fn fileGroupOwnerAccountName(&self) -> Option<Retained<NSString>>
NSFileManager
and NSString
only.pub fn fileSystemNumber(&self) -> NSInteger
NSFileManager
only.pub fn fileSystemFileNumber(&self) -> NSUInteger
NSFileManager
only.pub fn fileExtensionHidden(&self) -> bool
NSFileManager
only.pub fn fileHFSCreatorCode(&self) -> u32
NSFileManager
only.pub fn fileHFSTypeCode(&self) -> u32
NSFileManager
only.pub fn fileIsImmutable(&self) -> bool
NSFileManager
only.pub fn fileIsAppendOnly(&self) -> bool
NSFileManager
only.pub fn fileCreationDate(&self) -> Option<Retained<NSDate>>
NSFileManager
and NSDate
only.pub fn fileOwnerAccountID(&self) -> Option<Retained<NSNumber>>
NSFileManager
and NSValue
only.pub fn fileGroupOwnerAccountID(&self) -> Option<Retained<NSNumber>>
NSFileManager
and NSValue
only.pub fn valueForKey(&self, key: &NSString) -> Option<Retained<ObjectType>>
NSKeyValueCoding
and NSString
only.Methods from Deref<Target = NSObject>§
Sourcepub fn doesNotRecognizeSelector(&self, sel: Sel) -> !
pub fn doesNotRecognizeSelector(&self, sel: Sel) -> !
Handle messages the object doesn’t recognize.
See Apple’s documentation for details.
Methods from Deref<Target = AnyObject>§
Sourcepub fn class(&self) -> &'static AnyClass
pub fn class(&self) -> &'static AnyClass
Dynamically find the class of this object.
§Panics
May panic if the object is invalid (which may be the case for objects
returned from unavailable init
/new
methods).
§Example
Check that an instance of NSObject
has the precise class NSObject
.
use objc2::ClassType;
use objc2::runtime::NSObject;
let obj = NSObject::new();
assert_eq!(obj.class(), NSObject::class());
Sourcepub unsafe fn get_ivar<T>(&self, name: &str) -> &Twhere
T: Encode,
👎Deprecated: this is difficult to use correctly, use Ivar::load
instead.
pub unsafe fn get_ivar<T>(&self, name: &str) -> &Twhere
T: Encode,
Ivar::load
instead.Use Ivar::load
instead.
§Safety
The object must have an instance variable with the given name, and it
must be of type T
.
See Ivar::load_ptr
for details surrounding this.
Sourcepub fn downcast_ref<T>(&self) -> Option<&T>where
T: DowncastTarget,
pub fn downcast_ref<T>(&self) -> Option<&T>where
T: DowncastTarget,
Attempt to downcast the object to a class of type T
.
This is the reference-variant. Use Retained::downcast
if you want
to convert a retained object to another type.
§Mutable classes
Some classes have immutable and mutable variants, such as NSString
and NSMutableString
.
When some Objective-C API signature says it gives you an immutable class, it generally expects you to not mutate that, even though it may technically be mutable “under the hood”.
So using this method to convert a NSString
to a NSMutableString
,
while not unsound, is generally frowned upon unless you created the
string yourself, or the API explicitly documents the string to be
mutable.
See Apple’s documentation on mutability and on
isKindOfClass:
for more details.
§Generic classes
Objective-C generics are called “lightweight generics”, and that’s because they aren’t exposed in the runtime. This makes it impossible to safely downcast to generic collections, so this is disallowed by this method.
You can, however, safely downcast to generic collections where all the
type-parameters are AnyObject
.
§Panics
This works internally by calling isKindOfClass:
. That means that the
object must have the instance method of that name, and an exception
will be thrown (if CoreFoundation is linked) or the process will abort
if that is not the case. In the vast majority of cases, you don’t need
to worry about this, since both root objects NSObject
and
NSProxy
implement this method.
§Examples
Cast an NSString
back and forth from NSObject
.
use objc2::rc::Retained;
use objc2_foundation::{NSObject, NSString};
let obj: Retained<NSObject> = NSString::new().into_super();
let string = obj.downcast_ref::<NSString>().unwrap();
// Or with `downcast`, if we do not need the object afterwards
let string = obj.downcast::<NSString>().unwrap();
Try (and fail) to cast an NSObject
to an NSString
.
use objc2_foundation::{NSObject, NSString};
let obj = NSObject::new();
assert!(obj.downcast_ref::<NSString>().is_none());
Try to cast to an array of strings.
use objc2_foundation::{NSArray, NSObject, NSString};
let arr = NSArray::from_retained_slice(&[NSObject::new()]);
// This is invalid and doesn't type check.
let arr = arr.downcast_ref::<NSArray<NSString>>();
This fails to compile, since it would require enumerating over the array to ensure that each element is of the desired type, which is a performance pitfall.
Downcast when processing each element instead.
use objc2_foundation::{NSArray, NSObject, NSString};
let arr = NSArray::from_retained_slice(&[NSObject::new()]);
for elem in arr {
if let Some(data) = elem.downcast_ref::<NSString>() {
// handle `data`
}
}
Trait Implementations§
Source§impl<KeyType: ?Sized + Message, ObjectType: ?Sized + Message> AsRef<AnyObject> for NSMutableDictionary<KeyType, ObjectType>
impl<KeyType: ?Sized + Message, ObjectType: ?Sized + Message> AsRef<AnyObject> for NSMutableDictionary<KeyType, ObjectType>
Source§impl<KeyType: ?Sized + Message, ObjectType: ?Sized + Message> AsRef<CFMutableDictionary<KeyType, ObjectType>> for NSMutableDictionary<KeyType, ObjectType>
Available on crate feature objc2-core-foundation
only.
impl<KeyType: ?Sized + Message, ObjectType: ?Sized + Message> AsRef<CFMutableDictionary<KeyType, ObjectType>> for NSMutableDictionary<KeyType, ObjectType>
objc2-core-foundation
only.Source§fn as_ref(&self) -> &CFMutableDictionary<KeyType, ObjectType>
fn as_ref(&self) -> &CFMutableDictionary<KeyType, ObjectType>
Source§impl<KeyType: ?Sized + Message, ObjectType: ?Sized + Message> AsRef<NSDictionary<KeyType, ObjectType>> for NSMutableDictionary<KeyType, ObjectType>
impl<KeyType: ?Sized + Message, ObjectType: ?Sized + Message> AsRef<NSDictionary<KeyType, ObjectType>> for NSMutableDictionary<KeyType, ObjectType>
Source§fn as_ref(&self) -> &NSDictionary<KeyType, ObjectType>
fn as_ref(&self) -> &NSDictionary<KeyType, ObjectType>
Source§impl<KeyType: ?Sized + Message, ObjectType: ?Sized + Message> AsRef<NSMutableDictionary<KeyType, ObjectType>> for CFMutableDictionary<KeyType, ObjectType>
Available on crate feature objc2-core-foundation
only.
impl<KeyType: ?Sized + Message, ObjectType: ?Sized + Message> AsRef<NSMutableDictionary<KeyType, ObjectType>> for CFMutableDictionary<KeyType, ObjectType>
objc2-core-foundation
only.Source§fn as_ref(&self) -> &NSMutableDictionary<KeyType, ObjectType>
fn as_ref(&self) -> &NSMutableDictionary<KeyType, ObjectType>
Source§impl<KeyType: ?Sized + Message, ObjectType: ?Sized + Message> AsRef<NSMutableDictionary<KeyType, ObjectType>> for NSMutableDictionary<KeyType, ObjectType>
impl<KeyType: ?Sized + Message, ObjectType: ?Sized + Message> AsRef<NSMutableDictionary<KeyType, ObjectType>> for NSMutableDictionary<KeyType, ObjectType>
Source§impl<KeyType: ?Sized + Message, ObjectType: ?Sized + Message> AsRef<NSObject> for NSMutableDictionary<KeyType, ObjectType>
impl<KeyType: ?Sized + Message, ObjectType: ?Sized + Message> AsRef<NSObject> for NSMutableDictionary<KeyType, ObjectType>
Source§impl<KeyType: ?Sized + Message, ObjectType: ?Sized + Message> Borrow<AnyObject> for NSMutableDictionary<KeyType, ObjectType>
impl<KeyType: ?Sized + Message, ObjectType: ?Sized + Message> Borrow<AnyObject> for NSMutableDictionary<KeyType, ObjectType>
Source§impl<KeyType: ?Sized + Message, ObjectType: ?Sized + Message> Borrow<NSDictionary<KeyType, ObjectType>> for NSMutableDictionary<KeyType, ObjectType>
impl<KeyType: ?Sized + Message, ObjectType: ?Sized + Message> Borrow<NSDictionary<KeyType, ObjectType>> for NSMutableDictionary<KeyType, ObjectType>
Source§fn borrow(&self) -> &NSDictionary<KeyType, ObjectType>
fn borrow(&self) -> &NSDictionary<KeyType, ObjectType>
Source§impl<KeyType: ?Sized + Message, ObjectType: ?Sized + Message> Borrow<NSObject> for NSMutableDictionary<KeyType, ObjectType>
impl<KeyType: ?Sized + Message, ObjectType: ?Sized + Message> Borrow<NSObject> for NSMutableDictionary<KeyType, ObjectType>
Source§impl<KeyType: ?Sized + Message, ObjectType: ?Sized + Message> ClassType for NSMutableDictionary<KeyType, ObjectType>
impl<KeyType: ?Sized + Message, ObjectType: ?Sized + Message> ClassType for NSMutableDictionary<KeyType, ObjectType>
Source§const NAME: &'static str = "NSMutableDictionary"
const NAME: &'static str = "NSMutableDictionary"
Source§type Super = NSDictionary<KeyType, ObjectType>
type Super = NSDictionary<KeyType, ObjectType>
Source§type ThreadKind = <<NSMutableDictionary<KeyType, ObjectType> as ClassType>::Super as ClassType>::ThreadKind
type ThreadKind = <<NSMutableDictionary<KeyType, ObjectType> as ClassType>::Super as ClassType>::ThreadKind
Source§impl<KeyType: ?Sized + Message, ObjectType: ?Sized + Message> CopyingHelper for NSMutableDictionary<KeyType, ObjectType>
Available on crate feature NSObject
only.
impl<KeyType: ?Sized + Message, ObjectType: ?Sized + Message> CopyingHelper for NSMutableDictionary<KeyType, ObjectType>
NSObject
only.Source§type Result = NSDictionary<KeyType, ObjectType>
type Result = NSDictionary<KeyType, ObjectType>
Self
if the type has no
immutable counterpart. Read moreSource§impl<KeyType: Debug + Message, ObjectType: Debug + Message> Debug for NSMutableDictionary<KeyType, ObjectType>
impl<KeyType: Debug + Message, ObjectType: Debug + Message> Debug for NSMutableDictionary<KeyType, ObjectType>
Source§impl<KeyType: Message, ObjectType: Message> DefaultRetained for NSMutableDictionary<KeyType, ObjectType>
impl<KeyType: Message, ObjectType: Message> DefaultRetained for NSMutableDictionary<KeyType, ObjectType>
Source§impl<KeyType: ?Sized + Message, ObjectType: ?Sized + Message> Deref for NSMutableDictionary<KeyType, ObjectType>
impl<KeyType: ?Sized + Message, ObjectType: ?Sized + Message> Deref for NSMutableDictionary<KeyType, ObjectType>
Source§impl<KeyType: ?Sized + Message, ObjectType: ?Sized + Message> Message for NSMutableDictionary<KeyType, ObjectType>
impl<KeyType: ?Sized + Message, ObjectType: ?Sized + Message> Message for NSMutableDictionary<KeyType, ObjectType>
Source§impl<KeyType: ?Sized + Message, ObjectType: ?Sized + Message> MutableCopyingHelper for NSMutableDictionary<KeyType, ObjectType>
Available on crate feature NSObject
only.
impl<KeyType: ?Sized + Message, ObjectType: ?Sized + Message> MutableCopyingHelper for NSMutableDictionary<KeyType, ObjectType>
NSObject
only.Source§type Result = NSMutableDictionary<KeyType, ObjectType>
type Result = NSMutableDictionary<KeyType, ObjectType>
Self
if the type has no
mutable counterpart. Read moreSource§impl<KeyType: ?Sized + NSCoding, ObjectType: ?Sized + NSCoding> NSCoding for NSMutableDictionary<KeyType, ObjectType>
impl<KeyType: ?Sized + NSCoding, ObjectType: ?Sized + NSCoding> NSCoding for NSMutableDictionary<KeyType, ObjectType>
Source§unsafe fn encodeWithCoder(&self, coder: &NSCoder)
unsafe fn encodeWithCoder(&self, coder: &NSCoder)
NSObject
and NSCoder
only.Source§impl<KeyType: ?Sized, ObjectType: ?Sized> NSCopying for NSMutableDictionary<KeyType, ObjectType>
impl<KeyType: ?Sized, ObjectType: ?Sized> NSCopying for NSMutableDictionary<KeyType, ObjectType>
Source§impl<KeyType: ?Sized, ObjectType: ?Sized> NSFastEnumeration for NSMutableDictionary<KeyType, ObjectType>
impl<KeyType: ?Sized, ObjectType: ?Sized> NSFastEnumeration for NSMutableDictionary<KeyType, ObjectType>
Source§unsafe fn countByEnumeratingWithState_objects_count(
&self,
state: NonNull<NSFastEnumerationState>,
buffer: NonNull<*mut AnyObject>,
len: NSUInteger,
) -> NSUInteger
unsafe fn countByEnumeratingWithState_objects_count( &self, state: NonNull<NSFastEnumerationState>, buffer: NonNull<*mut AnyObject>, len: NSUInteger, ) -> NSUInteger
NSEnumerator
only.Source§impl<KeyType: ?Sized, ObjectType: ?Sized> NSMutableCopying for NSMutableDictionary<KeyType, ObjectType>
impl<KeyType: ?Sized, ObjectType: ?Sized> NSMutableCopying for NSMutableDictionary<KeyType, ObjectType>
Source§impl<KeyType: ?Sized, ObjectType: ?Sized> NSObjectProtocol for NSMutableDictionary<KeyType, ObjectType>
impl<KeyType: ?Sized, ObjectType: ?Sized> NSObjectProtocol for NSMutableDictionary<KeyType, ObjectType>
Source§fn isEqual(&self, other: Option<&AnyObject>) -> bool
fn isEqual(&self, other: Option<&AnyObject>) -> bool
Source§fn hash(&self) -> usize
fn hash(&self) -> usize
Source§fn isKindOfClass(&self, cls: &AnyClass) -> bool
fn isKindOfClass(&self, cls: &AnyClass) -> bool
Source§fn is_kind_of<T>(&self) -> bool
fn is_kind_of<T>(&self) -> bool
isKindOfClass
directly, or cast your objects with AnyObject::downcast_ref