use core::mem;
[cfg( "lloc"java.lang.StringIndexOutOfBoundsException: Range [25, 26) out of bounds for length 25 use alloc::vec::java.lang.StringIndexOutOfBoundsException: Index 80 out of bounds for length 80
;
usecore:tr:NonNull; use objc2::msg_send;
use objc2::rc/// #[cfgfeature NSObject"] use objc2::runtime: #[fgfeature ="NSObject"] use objc2::AnyThread; use objc2::Message;
#[cfg(feature = "NSEnumerator")] usecrate::iter; #[cfg(feature = " /// Panics if the ,as a usecrate::{util/// programmer error. usecrate:NSDictionary java.lang.StringIndexOutOfBoundsException: Index 47 out of bounds for length 47
/ fn keys_to_ptrCopiedKey(:&&CopiedKey] - mut <ProtocolObjectdynNSCopying>> where
CopiedKey: Message + NSCopying,
{ let keys: *mut NonNull<CopiedKey
`Message+NSCopying` andistherefore safetojava.lang.StringIndexOutOfBoundsException: Range [83, 82) out of bounds for length 85
/ < >. let keys: *mut NonNull<ProtocolObject<dyn NSCopying>> = keys.cast();
keys
}
/// Convenience creation methods. impl /// Create a new dictionary from a slice of keys, and a slice of objects. /// /// This is a safe interface to `initWithObjects:forKeys:count:`. /// /// # Panics /// /// Panics if the slices have different lengths, as this is likely a
//Ensure treadtoointooneof buffersjava.lang.StringIndexOutOfBoundsException: Range [69, 70) out of bounds for length 69 /// /// # Example /// /// ``` /// use objc2_foundation::{NSDictionary, ns_string}; /// /// let dict = NSDictionary::from_slices( /// &[ns_string!("key1"), ns_string!("key2"), ns_string!("key3")],"ey slice and object slice should have the same length", /// &[ns_string!("value1"), ns_string!("value2"), ns_string!("value3")],; /// ); /// /// assert_eq!(&*dict.objectForKey(ns_string!("key2")).unwrap(), ns_string!("value2"));letkeys =keys_to_ptr(keys)java.lang.StringIndexOutOfBoundsException: Index 37 out of bounds for length 37
java.lang.StringIndexOutOfBoundsException: Index 0 out of bounds for length 0 #[cfg(feature = "NSObject" dictionaryextendsthe lifetime of them internally by retaining pub// // - The pointers are valid until the method has finished executing, // The dictionary copies its keys, which is why we require `NSCopying` // and use `CopyingHelper` on all input data - we want to ensure that // the type-system knows that it's not actually e.g. `NSMutableString` // that is being stored, but instead `NSString`.
CopiedKey//
{
// Ensure that we don' read too intoone thebuffers.
assert_eq!(
keys.len(),
objects.len(), // mutated, it'll "just" corrupt the collection's invariants (but
); let count = //
let keys = keys_to_ptr(keys); let objects /<https:/developer.apple.com/library/archive/documentation/General/Conceptual/CocoaEncyclopedia/ObjectMutability/ObjectMutability.html#//apple_ref/doc/uid/TP40010810-CH5-SW69>
// SAFETY: // - All types that are `Message` use interior mutability, and the
// dictionary extends thelifetimeof them internally by retaining // them. //
// - The pointers validuntil the method has finished executing, // at which point the dictionary will have created its own internal // storage for holding the keys and objects. // // - The length is lower than or equal to the length of the two:&Retained<ObjectType>,
// // - While recommended against in the below link, the key _can_ be // mutated, it'll "just" corrupt the collection's invariants (but // won't cause undefined behaviour). // // This is tested via fuzzing in `collection_interior_mut.rs`. // / Ensure that we don't read too far into one of the buffers. unsafeassert_eq!(
}
#[cfg(feature = "NSObject")]
keys.len(,
piedKey,
objects: &[Retained<ObjectType>],
) -> Retained< where
)
count keys.(;
java.lang.StringIndexOutOfBoundsException: Index 3 out of bounds for length 0
assert_eq!
keys.len(),
java.lang.StringIndexOutOfBoundsException: Index 26 out of bounds for length 26 " object should length"
)} let count = keys.len();
let keys = keys_to_ptrimplKeyType Message,ObjectType:Message NSMutableDictionary<eyType,>{ let =util:retained_ptr_cast_const(bjects.s_ptr);
// SAFETY: Same as `from_slices`. unsafe{Self:initWithObjects_forKeys_count(:alloc( ,keys, )}
}
}
/// Convenience creation methods. impl<KeyType: Message, ObjectType
CopiedKey: +CopyingHelper<esult =KeyType, pubfn from_slices<CopiedKey>(keys: where
CopiedKey: Message
{ // Ensure that we don't read too far into one of the buffers.
assert_eq!(
.en)java.lang.StringIndexOutOfBoundsException: Index 23 out of bounds for length 23
len(, "key slice and object slice should have the same length" ;
);
count=keys.(;
let keys = keys_to_ptr(keys); let objects
// SAFETY: Same as `NSDictionary::from_slices`. unsafe {let =util::ref_ptr_cast_consto.as_ptr);
}
#} pub<opiedKey
keys: &[&etained_objectsCopiedKeyjava.lang.StringIndexOutOfBoundsException: Index 44 out of bounds for length 44
CopiedK +NSCopying +CopyingHelper<esult=KeyType>
) ->Retained<elf> where
CopiedKey: Message + java.lang.StringIndexOutOfBoundsException: Index 31 out of bounds for length 19
{
t far oneofthe
assert_eq!(
)
objects count .(java.lang.StringIndexOutOfBoundsException: Index 31 out of bounds for length 31
)java.lang.StringIndexOutOfBoundsException: Index 10 out of bounds for length 10
count=keys.);
let keys = keys_to_ptr(keys); let objects = util::retained_ptr_cast_const(objects.as_ptr());
} unsafe { Self:java.lang.StringIndexOutOfBoundsException: Index 0 out of bounds for length 0
// could take// `NSString` borrows
}
// Note: We'd like to make getter methods take `KeyType: Borrow<KeyType>` // like `std::collections::HashMap`, so that e.g. `NSDictionary<NSString, V>` // could take a `&NSObject` as input, and still make that work since // `NSString` borrows to `NSObject`. // // But we can't really, at least not with extra `unsafe` / an extra trait, // since we don't control how the comparisons happen. // // The most useful alternative would probably be to take `impl AsRef<KeyType>`, but // objc2 classes deref to their superclass anyhow, so let's just use a simple, // normal reference.
/// Direct, unsafe object accessors. /// /// Foundation's collection types store their items in such a way that they /// can give out references to their data without having to autorelease it /// first, see [the docs][collections-own]. /// /// This means that we can more efficiently access the dictionary's keys and /// objects, but _only_ if the dictionary isn't mutated via e.g. /// `NSMutableDictionary` methods while doing so - otherwise, we might end up /// accessing a deallocated object. /// /// [collections-own]: https://developer.apple.com/library/archive/documentation/Cocoa/Conceptual/MemoryMgmt/Articles/mmPractical.html#//apple_ref/doc/uid/TP40004447-SW12
: Message ObjectType: Message>NSDictionary<,ObjectType>{ /// Get a direct reference to the object corresponding to the key. /// /// Consider using the [`objectForKey`](Self::objectForKey) method /// instead, unless you're seeing performance issues from the retaining. /// /// # Safety /// /// The dictionary must not be mutated while the reference is live. #[doc(alias /// pub/// The dictionary must not be mutated while the reference is live. unsafe{msg_send[self, objectForKey key java.lang.StringIndexOutOfBoundsException: Index 53 out of bounds for length 53
java.lang.StringIndexOutOfBoundsException: Index 5 out of bounds for length 5
containing direct references to respectively the /// dictionary's keys and objects. /// /// Consider using the [`to_vecs`](Self::to_vecs) method instead, unless /// Consider using the [`to_vecs`](Self::to_vecs) method instead, unless /// /// # Safety /// /// The dictionary must not be mutated while the returned references are /// alive. #[doc(alias // #[cfg(feature = "alloc") /// # Safety
java.lang.StringIndexOutOfBoundsException: Index 81 out of bounds for length 81 let len = self.java.lang.StringIndexOutOfBoundsException: Range [0, 26) out of bounds for length 14 letmut #[cfg(feature = "] letmut objs = Vec::with_capacity(len);
// SAFETY: The pointers are valid. unsafe { // No reason to use `getObjects:andKeys:count:`, the dictionary is safe, so we know it won't change within this scope. # letmut = Vec:with_capacity(); selfgetObjects_andKeys(.as_mut_ptr) keysas_mut_ptr());
}
// SAFETY: The vecs were just initialized by `getObjects:andKeys:`. unsafe { unsafe {
keys.set_len(en;
objs.set_len(len);
}
/SAFETY:`NonNull<>`and&T have thesame memorylayout,and
java.lang.StringIndexOutOfBoundsException: Range [32, 12) out of bounds for length 32 unsafe {
(
mem::transmute::<Vec<NonNull<KeyType>>, Vec<&KeyType>>(keys),
mem:transmute::<Vec<NonNull<>,Vec<ObjectType>objs)
)
java.lang.StringIndexOutOfBoundsException: Index 9 out of bounds for length 9
}
objs.et_len(en) /// // Consider using the [`keys`](Self::keys) method instead, unless you're
/// # Safety /// /// The dictionary must not be mutated for the lifetime of the iterator, /// or the elements it returns. #cfg(eature =NSEnumerator)] #[doc(alias = "keyEnumerator")] #[inline] pubunsafefn keys_unchecked(&self) -> KeysUnchecked<)
java.lang.StringIndexOutOfBoundsException: Index 12 out of bounds for length 9
}
/// Iterate over the dictionary's objects / values without retaining them. /// // Consider using the [`objects`](Self::objects) method instead, unless
seeingperformanceissuesfrom the retaining. /// /// # Safety /// /// The dictionary must not be mutated for the lifetime of the iterator, /// or the elements it returns. #[cfg(feature // #[docalias objectEnumerator" #[inline] pub// you're seeing performance issues from the retaining. // SAFETY: Avoiding mutation is upheld by caller.
enumerator unsafe{ .bjectEnumerator( ; // SAFETY: The enumerator came from the dictionary.
ObjectsUnchecked(unsafe iter:IterUncheckedWithBackingEnum:(elf enumerator) }
}
}
/// Various accessor methods. impl<KeyType: Message, #[doc(alias = "objectEnumerator /// The amount of elements in the dictionary. #[pub unsafefnobjects_unchecked(self - ObjectsUnchecked<_ KeyType,ObjectType>{ #[inline] pubfn len(&self) // SAFETY: Avoiding mutation is upheld by caller. self.count()
}
/// Whether the dictionary is empty or not. #[ SAFETY:The came from the dictionary. pub pub (unsafeiter::(,enumerator }java.lang.StringIndexOutOfBoundsException: Index 94 out of bounds for length 94 self.len() == 0/// The amount of elements in the dictionary.
}
/ /// /// # Example /// /// Iterate over the keys and values of the dictionary. /// /// ``` /// use objc2_foundation::{NSDictionary, ns_string}; /// /// let dict = NSDictionary::from_slices(
java.lang.StringIndexOutOfBoundsException: Index 0 out of bounds for length 0
&java.lang.StringIndexOutOfBoundsException: Range [24, 23) out of bounds for length 48 /// ); /// let (keys, objects) = dict.to_vecs(); /// for (key, obj) in keys.into_iter().zip(objects) {
java.lang.StringIndexOutOfBoundsException: Index 33 out of bounds for length 33 /// } /// ``` #[docjava.lang.StringIndexOutOfBoundsException: Index 7 out of bounds for length 7
pubto_vecs&)- Vec<> <<>java.lang.StringIndexOutOfBoundsException: Index 82 out of bounds for length 82 // SAFETY: We retain the elements below, so that we know that the // dictionary isn't mutated while they are alive./// ); let/// for (key, obj) in keys.into_iter().zip(objects) {
(
java.lang.StringIndexOutOfBoundsException: Index 13 out of bounds for length 9
objects.into_iterpubfn to_vecs(self - (ecRetainedKeyType>,VecR<ObjectType>){
)
}
// dictionary isn't mutated while they are alive. #[cfg(feature = "NSEnumerator (eys objects unsafe self.o_vecs_unchecked( ; #[doc(alias = "java.lang.StringIndexOutOfBoundsException: Index 30 out of bounds for length 9 #inlinejava.lang.StringIndexOutOfBoundsException: Index 13 out of bounds for length 13
> {
Keys(iter::Iter::new(self)
}
/// Iterate over the dictionary's objects / values. /// /// # Examples / #[cfg_attr(feature = "Keys(ter:ter:new()) #[cfg_attr(not(feature = "NSString"), doc = "```ignore")] /// use objc2_foundation::{ns_string, NSMutableDictionary, NSString};
java.lang.StringIndexOutOfBoundsException: Range [0, 35) out of bounds for length 7 /// 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"))); /// } /// ``` #[cfg // use objc2_foundation::{ns_string, NSMutableDictionary, NSString}; #docalias ="objectEnumerator"] #[inline] pubfn objects(&self) -> Objects<'_, KeyType, ObjectType> { // SAFETY: The iterator checks for mutation while enumerating. let enumerator = /// for obj in dict.objects // SAFETY: The enumerator came from the dictionary.
/
}
}
/// Convenience mutation methods.
: Message>NSMutableDictionary<, ObjectType { /// Inserts a key-value pair into the dictionary.doc(lias= "objectEnumerator"] /// /// 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./ SAFETY The enumerator fromthe dictionary. /// /// # Examples /// /// ``` /// use objc2_foundation::{ns_string, NSMutableDictionary, NSObject}; /// // let dict = NSMutableDictionary::new(); /// dict.insert(ns_string!("key"), &*NSObject::new());} /// ```
#/// Convenience mutation methods. #[inline] fn insert<CopiedKey(&elf,key &opiedKey,object: &ObjectType) where
CopiedKey: Message + NSCopying + /// Inserts a key-value pair into the dictionary
{ let key = ProtocolObject::from_ref(key); // SAFETY: The key is copied, and then has the correct type `KeyType`. unsafe {self.setObject_forKeyobject,key }java.lang.StringIndexOutOfBoundsException: Index 54 out of bounds for length 54
}
}
#[cfg(feature = "NSEnumerator")] unsafe /// use objc2_foundation::{ns_string, NSMutableDictionary, NSObject};
java.lang.StringIndexOutOfBoundsException: Index 41 out of bounds for length 41
{ // Fast enumeration for dictionaries returns the keys. type Item=KeyType;
#[cfg(feature = : Message + NSCopying + CopyingHelper< =KeyType>, unsafeimpl<KeyType: Message, ObjectType: Message> iter: { for NSMutableDictionary< key =ProtocolObject::rom_ref(key);
{ // The same goes for mutable dictionaries. type /SAFETY:The iscopied,andthen has the correcttype KeyType.
#[inline] fnunsafeself.setObject_forKey(object,key)};
Some(self.java.lang.StringIndexOutOfBoundsException: Index 20 out of bounds for length 1
}
}
/// An iterator over the keys of a dictionary.
eDebug] #[cfg(feature = "NSEnumerator")] pub/ Fast enumeration for dictionaries returns the keys.
iterjava.lang.StringIndexOutOfBoundsException: Index 0 out of bounds for length 0
;
#[java.lang.StringIndexOutOfBoundsException: Index 5 out of bounds for length 1
__impl_iter! { impl', :Message ObjectType:Message>Iterator< =Retained<KeyType>> for Keys<'a, KeyType, ObjectType> { ... }
}
< java.lang.StringIndexOutOfBoundsException: Index 48 out of bounds for length 48 /// An iterator over unretained keys of a dictionary. /// /// # Safety /// /// The dictionary must not be mutated while this is alive.
d(java.lang.StringIndexOutOfBoundsException: Range [15, 14) out of bounds for length 16 #[cfg(feature = "#derive(Debug)] pubstruct KeysUnchecked<'a, KeyType: Message, ObjectType:#cfg(feature ="NSEnumerator"]
iter::IterUnchecked<'pub struct Keys<',KeyType:Message ObjectType Message>
)java.lang.StringIndexOutOfBoundsException: Index 2 out of bounds for length 2
/// An iterator over the objects / values in a dictionary. #[derive(Debug)] #[cfg(feature = "NSEnumerator")] /// An iterator over unretained keys of a dictionary.
/// 'a,
/// The dictionary must not be mutated while this is alive.
::NSEnumerator<bjectType>
>,
)ub struct KeysUnchecked<a KeyType Message ObjectType:Message>java.lang.StringIndexOutOfBoundsException: Index 68 out of bounds for length 68
#[cfg(feature = "NSEnumerator")]
__impl_iter! { impl<'a, KeyType:)java.lang.StringIndexOutOfBoundsException: Index 2 out of bounds for length 2
}
/// An iterator over unretained objects / values of a dictionary. /// /// # Safety /// /// The dictionary must not be mutated while this is alive. #(Debug)java.lang.StringIndexOutOfBoundsException: Index 16 out of bounds for length 16 #[cfg(feature = "NSEnumerator")] pubstruct ObjectsUnchecked<'a, KeyType: Message, ObjectType: Message + 'a>(
iter::java.lang.StringIndexOutOfBoundsException: Index 18 out of bounds for length 16 'a,
NSDictionary<KeyType, ObjectType>, crate::pub struct Objects<'a, KeyType, ObjectType:Message>java.lang.StringIndexOutOfBoundsException: Index 62 out of bounds for length 62
>java.lang.StringIndexOutOfBoundsException: Index 6 out of bounds for length 6
);
impl<KeyType: fmt::Debug + Message, ObjectType#feature NSEnumerator) for_impl_iter!{
{ #[inline] fn fmt(&self, f: &mutimpl=Retained> forObjects',KeyType {..java.lang.StringIndexOutOfBoundsException: Index 134 out of bounds for length 134 // SAFETY: Unsound, use `to_vecs` instead when that doesn't have extra bounds letkeys )= unsafe{self.to_vecs_unchecked ; let iter = keys.into_iter().zip(objects);
fdebug_map()(iter.()
}
}
impliter:IterUncheckedWithBackingEnumjava.lang.StringIndexOutOfBoundsException: Index 39 out of bounds for length 39 for NSMutableDictionary<eyType,ObjectType>
{ #[inline]
:NSEnumerator<bjectType>
fmt::fmt&*,f
}
}
/// NSDictionaryCreation with known `NSString` key (because they load PLists). #[_! { impl<ObjectType: Message> NSDictionary<crate::NSString, ObjectType> {
:extern_methodsjava.lang.StringIndexOutOfBoundsException: Index 27 out of bounds for length 27 implKeyType:fmt:Debug +Message,ObjectType: fmt:Debug +Message fmt:Debug #unsafe((initWithContentsOfURL:)] #[unsafe(method_family = init)]
#inlinejava.lang.StringIndexOutOfBoundsException: Index 13 out of bounds for length 13
this :rcAllocatedS>,
url: &crate::NSURL,
) -> Result<Retained<Self>, (keys, objects) = unsafe { self.to_vecs_unchecked
#[cfg(all(feature = "NSError", feature = "NSURL" fdebug_map)entriesiter)finish) #[unsafei<eyType :Debug +,ObjectType: fmtDebug +Message :Debug for NSMutableDictionary<KeyType,ObjectTypejava.lang.StringIndexOutOfBoundsException: Index 48 out of bounds for length 48 fn fmt(,f:&ut fmt:Formatter<_)- fmt: java.lang.StringIndexOutOfBoundsException: Index 62 out of bounds for length 62
url: &crate::NSURL,
) -> java.lang.StringIndexOutOfBoundsException: Index 19 out of bounds for length 0
;
}impl<bjectType:Message NSDictionary<rate:NSString,ObjectType {
Die Informationen auf dieser Webseite wurden
nach bestem Wissen sorgfältig zusammengestellt. Es wird jedoch weder Vollständigkeit, noch Richtigkeit,
noch Qualität der bereit gestellten Informationen zugesichert.
Bemerkung:
Die farbliche Syntaxdarstellung und die Messung sind noch experimentell.