#![cfg(feature = "NSArray")] #![cfg(feature = "NSValue")] #![cfg(feature = "NSRange")] use alloc::format; use alloc::vec::Vec; use core::ptr;
usecrate::{NSArray, NSNumber, NSObject, NSValue}; use objc2::extern_protocol; use objc2::rc::Retained; use objc2::runtime::{AnyObject, ProtocolObject};
let empty_array = <NSArray<NSObject>>::new();
assert!(empty_array.firstObject().is_none());
assert!(empty_array.lastObject().is_none());
}
#[test] fn test_iter() { let array = sample_number_array(4);
let vec1 = array.to_vec(); let vec2: Vec<_> = array.iter().collect();
assert_eq!(vec1, vec2);
letmut iterations = 0; for _ in &array {
iterations += 1;
} for _ in array.iter() {
iterations += 1;
} for _ inunsafe { array.iter_unchecked() } {
iterations += 1;
} for _ in array {
iterations += 1;
}
assert_eq!(iterations, 4 * 4);
}
#[test] fn test_iter_fused() { // Not actually documented, nor is FusedIterator implemented for the // array iterators; but it it still important to test that iterating past // the end still works.
let obj: Retained<ProtocolObject<dyn TestProtocol>> =
ProtocolObject::from_retained(NSNumber::new_i32(42)); let _ = NSArray::from_slice(&[&*obj, &*obj]); let _ = NSArray::from_retained_slice(&[obj.clone(), obj.clone()]);
}
#[test] fn test_access_anyobject() { let obj: Retained<AnyObject> = NSObject::new().into_super(); let array = NSArray::from_retained_slice(&[obj.clone(), obj.clone()]);
assert!(ptr::eq(&*array.objectAtIndex(0), &*obj));
assert!(ptr::eq(unsafe { array.objectAtIndex_unchecked(0) }, &*obj)); for _ in array.iter() {} for _ inunsafe { array.iter_unchecked() } {} for _ in array {}
}
#[test] fn test_cast() { let array = NSArray::from_retained_slice(&[NSNumber::new_i64(42)]); // SAFETY: NSNumber is a subclass of NSValue. let array = unsafe { array.cast_unchecked::<NSValue>() }; let value = array.objectAtIndex(0); // SAFETY: We put an i64 into the NSNumber.
assert_eq!(unsafe { value.get::<i64>() }, 42);
}
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.