Struct icrate::Foundation::NSArray
source · #[repr(C)]pub struct NSArray<ObjectType: Message = Object, ObjectTypeOwnership: Ownership = Shared> { /* private fields */ }Foundation only.Implementations§
source§impl<T: Message, O: Ownership> NSArray<T, O>
impl<T: Message, O: Ownership> NSArray<T, O>
Generic creation methods.
sourcepub fn from_vec(vec: Vec<Id<T, O>>) -> Id<Self, O>
pub fn from_vec(vec: Vec<Id<T, O>>) -> Id<Self, O>
Examples found in repository?
5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44
fn main() {
// Create and compare NSObjects
let obj = NSObject::new();
#[allow(clippy::eq_op)]
{
println!("{obj:?} == {obj:?}? {:?}", obj == obj);
}
let obj2 = NSObject::new();
println!("{obj:?} == {obj2:?}? {:?}", obj == obj2);
// Create an NSArray from a Vec
let objs = vec![obj, obj2];
let array = NSArray::from_vec(objs);
for obj in array.iter() {
println!("{obj:?}");
}
println!("{}", array.len());
// Turn the NSArray back into a Vec
let mut objs = NSArray::into_vec(array);
let obj = objs.pop().unwrap();
// Create a static NSString
let string = ns_string!("Hello, world!");
// Use an autoreleasepool to get the `str` contents of the NSString
autoreleasepool(|pool| {
println!("{}", string.as_str(pool));
});
// Or simply use the `Display` implementation
let _s = string.to_string(); // Using ToString
println!("{string}"); // Or Display directly
// Create a dictionary mapping strings to objects
let keys = &[string];
let vals = vec![obj];
let dict = NSDictionary::from_keys_and_objects(keys, vals);
println!("{:?}", dict.get(string));
println!("{}", dict.len());
}Creation methods that produce shared arrays.
sourcepub fn from_slice(slice: &[Id<T, Shared>]) -> Id<Self, Shared>
pub fn from_slice(slice: &[Id<T, Shared>]) -> Id<Self, Shared>
Examples found in repository?
64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123
pub fn text_impl_2(&self) -> Id<NSString, Shared> {
// The NSPasteboard API is a bit weird, it requires you to pass
// classes as objects, which `icrate::Foundation::NSArray` was not
// really made for - so we convert the class to an `Object` type
// instead. Also, we wrap it in `ManuallyDrop` because I'm not sure
// how classes handle `release` calls?
//
// TODO: Investigate and find a better way to express this in objc2.
let string_classes: ManuallyDrop<[Id<Object, Shared>; 1]> = {
let cls: *const Class = NSString::class();
let cls = cls as *mut Object;
unsafe { ManuallyDrop::new([Id::new(cls).unwrap()]) }
};
// Temporary, see https://github.com/rust-lang/rust-clippy/issues/9101
#[allow(unknown_lints, clippy::explicit_auto_deref)]
let class_array = NSArray::from_slice(&*string_classes);
let options = NSDictionary::new();
let objects = unsafe { self.read_objects_for_classes(&class_array, &options) };
// TODO: Should perhaps return Id<Object, Shared>?
let ptr: *const Object = objects.first().unwrap();
// And this part is weird as well, since we now have to convert the
// object into an NSString, which we know it to be since that's what
// we told `readObjectsForClasses:options:`.
let ptr = ptr as *mut NSString;
unsafe { Id::retain(ptr) }.unwrap()
}
/// Defined here to make it easier to declare which types are expected.
/// This is a common pattern that I can wholeheartedly recommend!
///
/// SAFETY: `class_array` must contain classes!
unsafe fn read_objects_for_classes(
&self,
class_array: &NSArray<Object, Shared>,
options: &NSDictionary<NSPasteboardReadingOptionKey, Object>,
) -> Id<NSArray<Object, Shared>, Shared> {
unsafe { msg_send_id![self, readObjectsForClasses: class_array, options: options] }
}
/// This takes `&self` even though `writeObjects:` would seem to mutate
/// the pasteboard. "What is going on?", you might rightfully ask!
///
/// We do this because we can't soundly get a mutable reference to the
/// global `NSPasteboard` instance, see [`NSPasteboard::general`].
///
/// This is sound because `NSPasteboard` contains `NSObject`, which in
/// turn contains `UnsafeCell`, allowing interior mutability.
///
/// <https://developer.apple.com/documentation/appkit/nspasteboard/1533599-clearcontents?language=objc>
/// <https://developer.apple.com/documentation/appkit/nspasteboard/1525945-writeobjects?language=objc>
pub fn set_text(&self, text: Id<NSString, Shared>) {
let _: NSInteger = unsafe { msg_send![self, clearContents] };
let string_array = NSArray::from_slice(&[text]);
let res: bool = unsafe { msg_send![self, writeObjects: &*string_array] };
if !res {
panic!("Failed writing to pasteboard");
}
}source§impl<T: Message, O: Ownership> NSArray<T, O>
impl<T: Message, O: Ownership> NSArray<T, O>
Generic accessor methods.
sourcepub fn len(&self) -> usize
pub fn len(&self) -> usize
Examples found in repository?
5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44
fn main() {
// Create and compare NSObjects
let obj = NSObject::new();
#[allow(clippy::eq_op)]
{
println!("{obj:?} == {obj:?}? {:?}", obj == obj);
}
let obj2 = NSObject::new();
println!("{obj:?} == {obj2:?}? {:?}", obj == obj2);
// Create an NSArray from a Vec
let objs = vec![obj, obj2];
let array = NSArray::from_vec(objs);
for obj in array.iter() {
println!("{obj:?}");
}
println!("{}", array.len());
// Turn the NSArray back into a Vec
let mut objs = NSArray::into_vec(array);
let obj = objs.pop().unwrap();
// Create a static NSString
let string = ns_string!("Hello, world!");
// Use an autoreleasepool to get the `str` contents of the NSString
autoreleasepool(|pool| {
println!("{}", string.as_str(pool));
});
// Or simply use the `Display` implementation
let _s = string.to_string(); // Using ToString
println!("{string}"); // Or Display directly
// Create a dictionary mapping strings to objects
let keys = &[string];
let vals = vec![obj];
let dict = NSDictionary::from_keys_and_objects(keys, vals);
println!("{:?}", dict.get(string));
println!("{}", dict.len());
}More examples
101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196
pub fn is_empty(&self) -> bool {
self.len() == 0
}
#[method(objectAtIndex:)]
unsafe fn get_unchecked(&self, index: usize) -> &T;
#[doc(alias = "objectAtIndex:")]
pub fn get(&self, index: usize) -> Option<&T> {
// TODO: Replace this check with catching the thrown NSRangeException
if index < self.len() {
// SAFETY: The index is checked to be in bounds.
Some(unsafe { self.get_unchecked(index) })
} else {
None
}
}
#[doc(alias = "firstObject")]
#[method(firstObject)]
pub fn first(&self) -> Option<&T>;
#[doc(alias = "lastObject")]
#[method(lastObject)]
pub fn last(&self) -> Option<&T>;
#[doc(alias = "objectEnumerator")]
pub fn iter(&self) -> NSEnumerator2<'_, T> {
unsafe {
let result: *mut Object = msg_send![self, objectEnumerator];
NSEnumerator2::from_ptr(result)
}
}
unsafe fn objects_in_range_unchecked(&self, range: Range<usize>) -> Vec<&T> {
let range = NSRange::from(range);
let mut vec: Vec<NonNull<T>> = Vec::with_capacity(range.length);
unsafe {
self.getObjects_range(NonNull::new(vec.as_mut_ptr()).unwrap(), range);
vec.set_len(range.length);
mem::transmute(vec)
}
}
pub fn objects_in_range(&self, range: Range<usize>) -> Option<Vec<&T>> {
if range.end > self.len() {
return None;
}
// SAFETY: Just checked that the range is in bounds
Some(unsafe { self.objects_in_range_unchecked(range) })
}
pub fn to_vec(&self) -> Vec<&T> {
// SAFETY: The range is know to be in bounds
unsafe { self.objects_in_range_unchecked(0..self.len()) }
}
// TODO: Take Id<Self, Self::ItemOwnership> ?
pub fn into_vec(array: Id<Self, Owned>) -> Vec<Id<T, O>> {
array
.to_vec()
.into_iter()
.map(|obj| unsafe { Id::retain(obj as *const T as *mut T).unwrap_unchecked() })
.collect()
}
}
/// Accessor methods that work on shared arrays.
unsafe impl<T: Message> NSArray<T, Shared> {
#[doc(alias = "objectAtIndex:")]
pub fn get_retained(&self, index: usize) -> Id<T, Shared> {
let obj = self.get(index).unwrap();
// SAFETY: The object is originally shared (see `where` bound).
unsafe { Id::retain_autoreleased(obj as *const T as *mut T).unwrap_unchecked() }
}
pub fn to_shared_vec(&self) -> Vec<Id<T, Shared>> {
self.to_vec()
.into_iter()
.map(|obj| unsafe { Id::retain(obj as *const T as *mut T).unwrap_unchecked() })
.collect()
}
}
/// Accessor methods that work on owned arrays.
unsafe impl<T: Message> NSArray<T, Owned> {
#[doc(alias = "objectAtIndex:")]
pub fn get_mut(&mut self, index: usize) -> Option<&mut T> {
// TODO: Replace this check with catching the thrown NSRangeException
if index < self.len() {
// SAFETY: The index is checked to be in bounds.
Some(unsafe { msg_send![self, objectAtIndex: index] })
} else {
None
}
}49 50 51 52 53 54 55 56 57 58 59 60 61 62
pub fn insert(&mut self, index: usize, obj: Id<T, O>) {
// TODO: Replace this check with catching the thrown NSRangeException
let len = self.len();
if index < len {
// SAFETY: The object has correct ownership, and the index is
// checked to be in bounds.
unsafe { self.insertObject_atIndex(&obj, index) }
} else {
panic!(
"insertion index (is {}) should be <= len (is {})",
index, len
);
}
}pub fn is_empty(&self) -> bool
sourcepub fn get(&self, index: usize) -> Option<&T>
pub fn get(&self, index: usize) -> Option<&T>
Examples found in repository?
171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246
pub fn get_retained(&self, index: usize) -> Id<T, Shared> {
let obj = self.get(index).unwrap();
// SAFETY: The object is originally shared (see `where` bound).
unsafe { Id::retain_autoreleased(obj as *const T as *mut T).unwrap_unchecked() }
}
pub fn to_shared_vec(&self) -> Vec<Id<T, Shared>> {
self.to_vec()
.into_iter()
.map(|obj| unsafe { Id::retain(obj as *const T as *mut T).unwrap_unchecked() })
.collect()
}
}
/// Accessor methods that work on owned arrays.
unsafe impl<T: Message> NSArray<T, Owned> {
#[doc(alias = "objectAtIndex:")]
pub fn get_mut(&mut self, index: usize) -> Option<&mut T> {
// TODO: Replace this check with catching the thrown NSRangeException
if index < self.len() {
// SAFETY: The index is checked to be in bounds.
Some(unsafe { msg_send![self, objectAtIndex: index] })
} else {
None
}
}
#[doc(alias = "firstObject")]
#[method(firstObject)]
pub fn first_mut(&mut self) -> Option<&mut T>;
#[doc(alias = "lastObject")]
#[method(lastObject)]
pub fn last_mut(&mut self) -> Option<&mut T>;
}
);
/// This is implemented as a shallow copy.
///
/// As such, it is only possible when the array's contents are `Shared`.
unsafe impl<T: Message> NSCopying for NSArray<T, Shared> {
type Ownership = Shared;
type Output = NSArray<T, Shared>;
}
/// This is implemented as a shallow copy.
unsafe impl<T: Message> NSMutableCopying for NSArray<T, Shared> {
type Output = NSMutableArray<T, Shared>;
}
impl<T: Message> alloc::borrow::ToOwned for NSArray<T, Shared> {
type Owned = Id<NSArray<T, Shared>, Shared>;
fn to_owned(&self) -> Self::Owned {
self.copy()
}
}
unsafe impl<T: Message, O: Ownership> NSFastEnumeration2 for NSArray<T, O> {
type Item = T;
}
impl<'a, T: Message, O: Ownership> IntoIterator for &'a NSArray<T, O> {
type Item = &'a T;
type IntoIter = NSFastEnumerator2<'a, NSArray<T, O>>;
fn into_iter(self) -> Self::IntoIter {
self.iter_fast()
}
}
impl<T: Message, O: Ownership> Index<usize> for NSArray<T, O> {
type Output = T;
fn index(&self, index: usize) -> &T {
self.get(index).unwrap()
}More examples
65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177
pub fn replace(&mut self, index: usize, obj: Id<T, O>) -> Id<T, O> {
let old_obj = unsafe {
let obj = self.get(index).unwrap();
Id::retain_autoreleased(obj as *const T as *mut T).unwrap_unchecked()
};
// SAFETY: The object has correct ownership.
unsafe { self.replaceObjectAtIndex_withObject(index, &obj) };
old_obj
}
#[doc(alias = "removeObjectAtIndex:")]
pub fn remove(&mut self, index: usize) -> Id<T, O> {
let obj = if let Some(obj) = self.get(index) {
unsafe { Id::retain_autoreleased(obj as *const T as *mut T).unwrap_unchecked() }
} else {
panic!("removal index should be < len");
};
// SAFETY: The index is checked to be in bounds.
unsafe { self.removeObjectAtIndex(index) };
obj
}
#[doc(alias = "removeLastObject")]
pub fn pop(&mut self) -> Option<Id<T, O>> {
self.last()
.map(|obj| unsafe { Id::retain(obj as *const T as *mut T).unwrap_unchecked() })
.map(|obj| {
// SAFETY: `Self::last` just checked that there is an object
unsafe { self.removeLastObject() };
obj
})
}
#[doc(alias = "sortUsingFunction:context:")]
pub fn sort_by<F: FnMut(&T, &T) -> Ordering>(&mut self, compare: F) {
// TODO: "C-unwind"
unsafe extern "C" fn compare_with_closure<U, F: FnMut(&U, &U) -> Ordering>(
obj1: NonNull<U>,
obj2: NonNull<U>,
context: *mut c_void,
) -> NSInteger {
let context: *mut F = context.cast();
// Bring back a reference to the closure.
// Guaranteed to be unique, we gave `sortUsingFunction` unique is
// ownership, and that method only runs one function at a time.
let closure: &mut F = unsafe { context.as_mut().unwrap_unchecked() };
// SAFETY: The objects are guaranteed to be valid
let (obj1, obj2) = unsafe { (obj1.as_ref(), obj2.as_ref()) };
NSComparisonResult::from((*closure)(obj1, obj2)) as _
}
// Create function pointer
let f: unsafe extern "C" fn(_, _, _) -> _ = compare_with_closure::<T, F>;
// Grab a type-erased pointer to the closure (a pointer to stack).
let mut closure = compare;
let context: *mut F = &mut closure;
unsafe { self.sortUsingFunction_context(f, context.cast()) };
// Keep the closure alive until the function has run.
drop(closure);
}
}
);
// Copying only possible when ItemOwnership = Shared
/// This is implemented as a shallow copy.
unsafe impl<T: Message> NSCopying for NSMutableArray<T, Shared> {
type Ownership = Shared;
type Output = NSArray<T, Shared>;
}
/// This is implemented as a shallow copy.
unsafe impl<T: Message> NSMutableCopying for NSMutableArray<T, Shared> {
type Output = NSMutableArray<T, Shared>;
}
impl<T: Message> alloc::borrow::ToOwned for NSMutableArray<T, Shared> {
type Owned = Id<NSMutableArray<T, Shared>, Owned>;
fn to_owned(&self) -> Self::Owned {
self.mutable_copy()
}
}
unsafe impl<T: Message, O: Ownership> NSFastEnumeration2 for NSMutableArray<T, O> {
type Item = T;
}
impl<'a, T: Message, O: Ownership> IntoIterator for &'a NSMutableArray<T, O> {
type Item = &'a T;
type IntoIter = NSFastEnumerator2<'a, NSMutableArray<T, O>>;
fn into_iter(self) -> Self::IntoIter {
self.iter_fast()
}
}
impl<T: Message, O: Ownership> Extend<Id<T, O>> for NSMutableArray<T, O> {
fn extend<I: IntoIterator<Item = Id<T, O>>>(&mut self, iter: I) {
let iterator = iter.into_iter();
iterator.for_each(move |item| self.push(item));
}
}
impl<T: Message, O: Ownership> Index<usize> for NSMutableArray<T, O> {
type Output = T;
fn index(&self, index: usize) -> &T {
self.get(index).unwrap()
}sourcepub fn first(&self) -> Option<&T>
pub fn first(&self) -> Option<&T>
Examples found in repository?
64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91
pub fn text_impl_2(&self) -> Id<NSString, Shared> {
// The NSPasteboard API is a bit weird, it requires you to pass
// classes as objects, which `icrate::Foundation::NSArray` was not
// really made for - so we convert the class to an `Object` type
// instead. Also, we wrap it in `ManuallyDrop` because I'm not sure
// how classes handle `release` calls?
//
// TODO: Investigate and find a better way to express this in objc2.
let string_classes: ManuallyDrop<[Id<Object, Shared>; 1]> = {
let cls: *const Class = NSString::class();
let cls = cls as *mut Object;
unsafe { ManuallyDrop::new([Id::new(cls).unwrap()]) }
};
// Temporary, see https://github.com/rust-lang/rust-clippy/issues/9101
#[allow(unknown_lints, clippy::explicit_auto_deref)]
let class_array = NSArray::from_slice(&*string_classes);
let options = NSDictionary::new();
let objects = unsafe { self.read_objects_for_classes(&class_array, &options) };
// TODO: Should perhaps return Id<Object, Shared>?
let ptr: *const Object = objects.first().unwrap();
// And this part is weird as well, since we now have to convert the
// object into an NSString, which we know it to be since that's what
// we told `readObjectsForClasses:options:`.
let ptr = ptr as *mut NSString;
unsafe { Id::retain(ptr) }.unwrap()
}sourcepub fn iter(&self) -> NSEnumerator2<'_, T> ⓘ
pub fn iter(&self) -> NSEnumerator2<'_, T> ⓘ
Examples found in repository?
5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44
fn main() {
// Create and compare NSObjects
let obj = NSObject::new();
#[allow(clippy::eq_op)]
{
println!("{obj:?} == {obj:?}? {:?}", obj == obj);
}
let obj2 = NSObject::new();
println!("{obj:?} == {obj2:?}? {:?}", obj == obj2);
// Create an NSArray from a Vec
let objs = vec![obj, obj2];
let array = NSArray::from_vec(objs);
for obj in array.iter() {
println!("{obj:?}");
}
println!("{}", array.len());
// Turn the NSArray back into a Vec
let mut objs = NSArray::into_vec(array);
let obj = objs.pop().unwrap();
// Create a static NSString
let string = ns_string!("Hello, world!");
// Use an autoreleasepool to get the `str` contents of the NSString
autoreleasepool(|pool| {
println!("{}", string.as_str(pool));
});
// Or simply use the `Display` implementation
let _s = string.to_string(); // Using ToString
println!("{string}"); // Or Display directly
// Create a dictionary mapping strings to objects
let keys = &[string];
let vals = vec![obj];
let dict = NSDictionary::from_keys_and_objects(keys, vals);
println!("{:?}", dict.get(string));
println!("{}", dict.len());
}pub fn objects_in_range(&self, range: Range<usize>) -> Option<Vec<&T>>
sourcepub fn to_vec(&self) -> Vec<&T>
pub fn to_vec(&self) -> Vec<&T>
Examples found in repository?
159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182
pub fn into_vec(array: Id<Self, Owned>) -> Vec<Id<T, O>> {
array
.to_vec()
.into_iter()
.map(|obj| unsafe { Id::retain(obj as *const T as *mut T).unwrap_unchecked() })
.collect()
}
}
/// Accessor methods that work on shared arrays.
unsafe impl<T: Message> NSArray<T, Shared> {
#[doc(alias = "objectAtIndex:")]
pub fn get_retained(&self, index: usize) -> Id<T, Shared> {
let obj = self.get(index).unwrap();
// SAFETY: The object is originally shared (see `where` bound).
unsafe { Id::retain_autoreleased(obj as *const T as *mut T).unwrap_unchecked() }
}
pub fn to_shared_vec(&self) -> Vec<Id<T, Shared>> {
self.to_vec()
.into_iter()
.map(|obj| unsafe { Id::retain(obj as *const T as *mut T).unwrap_unchecked() })
.collect()
}sourcepub fn into_vec(array: Id<Self, Owned>) -> Vec<Id<T, O>>
pub fn into_vec(array: Id<Self, Owned>) -> Vec<Id<T, O>>
Examples found in repository?
5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44
fn main() {
// Create and compare NSObjects
let obj = NSObject::new();
#[allow(clippy::eq_op)]
{
println!("{obj:?} == {obj:?}? {:?}", obj == obj);
}
let obj2 = NSObject::new();
println!("{obj:?} == {obj2:?}? {:?}", obj == obj2);
// Create an NSArray from a Vec
let objs = vec![obj, obj2];
let array = NSArray::from_vec(objs);
for obj in array.iter() {
println!("{obj:?}");
}
println!("{}", array.len());
// Turn the NSArray back into a Vec
let mut objs = NSArray::into_vec(array);
let obj = objs.pop().unwrap();
// Create a static NSString
let string = ns_string!("Hello, world!");
// Use an autoreleasepool to get the `str` contents of the NSString
autoreleasepool(|pool| {
println!("{}", string.as_str(pool));
});
// Or simply use the `Display` implementation
let _s = string.to_string(); // Using ToString
println!("{string}"); // Or Display directly
// Create a dictionary mapping strings to objects
let keys = &[string];
let vals = vec![obj];
let dict = NSDictionary::from_keys_and_objects(keys, vals);
println!("{:?}", dict.get(string));
println!("{}", dict.len());
}Accessor methods that work on shared arrays.
pub fn get_retained(&self, index: usize) -> Id<T, Shared>
source§impl<ObjectType: Message, ObjectTypeOwnership: Ownership> NSArray<ObjectType, ObjectTypeOwnership>
impl<ObjectType: Message, ObjectTypeOwnership: Ownership> NSArray<ObjectType, ObjectTypeOwnership>
NSArrayCreation
source§impl<ObjectType: Message, ObjectTypeOwnership: Ownership> NSArray<ObjectType, ObjectTypeOwnership>
impl<ObjectType: Message, ObjectTypeOwnership: Ownership> NSArray<ObjectType, ObjectTypeOwnership>
NSDeprecated
source§impl<ObjectType: Message, ObjectTypeOwnership: Ownership> NSArray<ObjectType, ObjectTypeOwnership>
impl<ObjectType: Message, ObjectTypeOwnership: Ownership> NSArray<ObjectType, ObjectTypeOwnership>
sourcepub fn count(&self) -> NSUInteger
pub fn count(&self) -> NSUInteger
pub unsafe fn objectAtIndex(&self, index: NSUInteger) -> Id<ObjectType, Shared>
pub unsafe fn init(this: Option<Allocated<Self>>) -> Id<Self, Shared>
pub unsafe fn initWithObjects_count(
this: Option<Allocated<Self>>,
objects: *mut NonNull<ObjectType>,
cnt: NSUInteger
) -> Id<Self, Shared>
pub unsafe fn initWithCoder(
this: Option<Allocated<Self>>,
coder: &NSCoder
) -> Option<Id<Self, Shared>>
source§impl<ObjectType: Message, ObjectTypeOwnership: Ownership> NSArray<ObjectType, ObjectTypeOwnership>
impl<ObjectType: Message, ObjectTypeOwnership: Ownership> NSArray<ObjectType, ObjectTypeOwnership>
NSExtendedArray
pub unsafe fn arrayByAddingObject(
&self,
anObject: &ObjectType
) -> Id<NSArray<ObjectType>, Shared>
pub unsafe fn arrayByAddingObjectsFromArray(
&self,
otherArray: &NSArray<ObjectType>
) -> Id<NSArray<ObjectType>, Shared>
pub unsafe fn componentsJoinedByString(
&self,
separator: &NSString
) -> Id<NSString, Shared>
pub unsafe fn containsObject(&self, anObject: &ObjectType) -> bool
pub unsafe fn description(&self) -> Id<NSString, Shared>
pub unsafe fn descriptionWithLocale(
&self,
locale: Option<&Object>
) -> Id<NSString, Shared>
pub unsafe fn descriptionWithLocale_indent(
&self,
locale: Option<&Object>,
level: NSUInteger
) -> Id<NSString, Shared>
pub unsafe fn firstObjectCommonWithArray(
&self,
otherArray: &NSArray<ObjectType>
) -> Option<Id<ObjectType, Shared>>
sourcepub unsafe fn getObjects_range(
&self,
objects: NonNull<NonNull<ObjectType>>,
range: NSRange
)
pub unsafe fn getObjects_range(
&self,
objects: NonNull<NonNull<ObjectType>>,
range: NSRange
)
pub unsafe fn indexOfObject(&self, anObject: &ObjectType) -> NSUInteger
pub unsafe fn indexOfObject_inRange(
&self,
anObject: &ObjectType,
range: NSRange
) -> NSUInteger
pub unsafe fn indexOfObjectIdenticalTo(
&self,
anObject: &ObjectType
) -> NSUInteger
pub unsafe fn indexOfObjectIdenticalTo_inRange(
&self,
anObject: &ObjectType,
range: NSRange
) -> NSUInteger
pub unsafe fn isEqualToArray(&self, otherArray: &NSArray<ObjectType>) -> bool
pub unsafe fn firstObject(&self) -> Option<Id<ObjectType, Shared>>
pub unsafe fn lastObject(&self) -> Option<Id<ObjectType, Shared>>
pub unsafe fn objectEnumerator(&self) -> Id<NSEnumerator<ObjectType>, Shared>
pub unsafe fn reverseObjectEnumerator(
&self
) -> Id<NSEnumerator<ObjectType>, Shared>
pub unsafe fn sortedArrayHint(&self) -> Id<NSData, Shared>
pub unsafe fn sortedArrayUsingFunction_context(
&self,
comparator: unsafe extern "C" fn(_: NonNull<ObjectType>, _: NonNull<ObjectType>, _: *mut c_void) -> NSInteger,
context: *mut c_void
) -> Id<NSArray<ObjectType>, Shared>
pub unsafe fn sortedArrayUsingFunction_context_hint(
&self,
comparator: unsafe extern "C" fn(_: NonNull<ObjectType>, _: NonNull<ObjectType>, _: *mut c_void) -> NSInteger,
context: *mut c_void,
hint: Option<&NSData>
) -> Id<NSArray<ObjectType>, Shared>
pub unsafe fn sortedArrayUsingSelector(
&self,
comparator: Sel
) -> Id<NSArray<ObjectType>, Shared>
pub unsafe fn subarrayWithRange(
&self,
range: NSRange
) -> Id<NSArray<ObjectType>, Shared>
pub unsafe fn writeToURL_error(
&self,
url: &NSURL
) -> Result<(), Id<NSError, Shared>>
pub unsafe fn makeObjectsPerformSelector(&self, aSelector: Sel)
pub unsafe fn makeObjectsPerformSelector_withObject(
&self,
aSelector: Sel,
argument: Option<&Object>
)
pub unsafe fn objectsAtIndexes(
&self,
indexes: &NSIndexSet
) -> Id<NSArray<ObjectType>, Shared>
pub unsafe fn objectAtIndexedSubscript(
&self,
idx: NSUInteger
) -> Id<ObjectType, Shared>
pub unsafe fn enumerateObjectsUsingBlock(
&self,
block: &Block<(NonNull<ObjectType>, NSUInteger, NonNull<Bool>), ()>
)
pub unsafe fn enumerateObjectsWithOptions_usingBlock(
&self,
opts: NSEnumerationOptions,
block: &Block<(NonNull<ObjectType>, NSUInteger, NonNull<Bool>), ()>
)
pub unsafe fn enumerateObjectsAtIndexes_options_usingBlock(
&self,
s: &NSIndexSet,
opts: NSEnumerationOptions,
block: &Block<(NonNull<ObjectType>, NSUInteger, NonNull<Bool>), ()>
)
pub unsafe fn indexOfObjectPassingTest(
&self,
predicate: &Block<(NonNull<ObjectType>, NSUInteger, NonNull<Bool>), Bool>
) -> NSUInteger
pub unsafe fn indexOfObjectWithOptions_passingTest(
&self,
opts: NSEnumerationOptions,
predicate: &Block<(NonNull<ObjectType>, NSUInteger, NonNull<Bool>), Bool>
) -> NSUInteger
pub unsafe fn indexOfObjectAtIndexes_options_passingTest(
&self,
s: &NSIndexSet,
opts: NSEnumerationOptions,
predicate: &Block<(NonNull<ObjectType>, NSUInteger, NonNull<Bool>), Bool>
) -> NSUInteger
pub unsafe fn indexesOfObjectsPassingTest(
&self,
predicate: &Block<(NonNull<ObjectType>, NSUInteger, NonNull<Bool>), Bool>
) -> Id<NSIndexSet, Shared>
pub unsafe fn indexesOfObjectsWithOptions_passingTest(
&self,
opts: NSEnumerationOptions,
predicate: &Block<(NonNull<ObjectType>, NSUInteger, NonNull<Bool>), Bool>
) -> Id<NSIndexSet, Shared>
pub unsafe fn indexesOfObjectsAtIndexes_options_passingTest(
&self,
s: &NSIndexSet,
opts: NSEnumerationOptions,
predicate: &Block<(NonNull<ObjectType>, NSUInteger, NonNull<Bool>), Bool>
) -> Id<NSIndexSet, Shared>
pub unsafe fn sortedArrayUsingComparator(
&self,
cmptr: NSComparator
) -> Id<NSArray<ObjectType>, Shared>
pub unsafe fn sortedArrayWithOptions_usingComparator(
&self,
opts: NSSortOptions,
cmptr: NSComparator
) -> Id<NSArray<ObjectType>, Shared>
pub unsafe fn indexOfObject_inSortedRange_options_usingComparator(
&self,
obj: &ObjectType,
r: NSRange,
opts: NSBinarySearchingOptions,
cmp: NSComparator
) -> NSUInteger
source§impl<ObjectType: Message, ObjectTypeOwnership: Ownership> NSArray<ObjectType, ObjectTypeOwnership>
impl<ObjectType: Message, ObjectTypeOwnership: Ownership> NSArray<ObjectType, ObjectTypeOwnership>
NSArrayCreation
pub unsafe fn array() -> Id<Self, Shared>
pub unsafe fn arrayWithObject(anObject: &ObjectType) -> Id<Self, Shared>
pub unsafe fn arrayWithObjects_count(
objects: NonNull<NonNull<ObjectType>>,
cnt: NSUInteger
) -> Id<Self, Shared>
pub unsafe fn arrayWithArray(array: &NSArray<ObjectType>) -> Id<Self, Shared>
pub unsafe fn initWithArray(
this: Option<Allocated<Self>>,
array: &NSArray<ObjectType>
) -> Id<Self, Shared>
pub unsafe fn initWithArray_copyItems(
this: Option<Allocated<Self>>,
array: &NSArray<ObjectType>,
flag: bool
) -> Id<Self, Shared>
pub unsafe fn arrayWithContentsOfURL_error(
url: &NSURL
) -> Result<Id<NSArray<ObjectType>, Shared>, Id<NSError, Shared>>
source§impl<ObjectType: Message, ObjectTypeOwnership: Ownership> NSArray<ObjectType, ObjectTypeOwnership>
impl<ObjectType: Message, ObjectTypeOwnership: Ownership> NSArray<ObjectType, ObjectTypeOwnership>
NSArrayDiffing
pub unsafe fn differenceFromArray_withOptions_usingEquivalenceTest(
&self,
other: &NSArray<ObjectType>,
options: NSOrderedCollectionDifferenceCalculationOptions,
block: &Block<(NonNull<ObjectType>, NonNull<ObjectType>), Bool>
) -> Id<NSOrderedCollectionDifference<ObjectType>, Shared>
pub unsafe fn differenceFromArray_withOptions(
&self,
other: &NSArray<ObjectType>,
options: NSOrderedCollectionDifferenceCalculationOptions
) -> Id<NSOrderedCollectionDifference<ObjectType>, Shared>
pub unsafe fn differenceFromArray(
&self,
other: &NSArray<ObjectType>
) -> Id<NSOrderedCollectionDifference<ObjectType>, Shared>
pub unsafe fn arrayByApplyingDifference(
&self,
difference: &NSOrderedCollectionDifference<ObjectType>
) -> Option<Id<NSArray<ObjectType>, Shared>>
source§impl<ObjectType: Message, ObjectTypeOwnership: Ownership> NSArray<ObjectType, ObjectTypeOwnership>
impl<ObjectType: Message, ObjectTypeOwnership: Ownership> NSArray<ObjectType, ObjectTypeOwnership>
NSDeprecated
pub unsafe fn getObjects(&self, objects: NonNull<NonNull<ObjectType>>)
pub unsafe fn arrayWithContentsOfFile(
path: &NSString
) -> Option<Id<NSArray<ObjectType>, Shared>>
pub unsafe fn arrayWithContentsOfURL(
url: &NSURL
) -> Option<Id<NSArray<ObjectType>, Shared>>
pub unsafe fn writeToFile_atomically(
&self,
path: &NSString,
useAuxiliaryFile: bool
) -> bool
pub unsafe fn writeToURL_atomically(&self, url: &NSURL, atomically: bool) -> bool
source§impl<ObjectType: Message, ObjectTypeOwnership: Ownership> NSArray<ObjectType, ObjectTypeOwnership>
impl<ObjectType: Message, ObjectTypeOwnership: Ownership> NSArray<ObjectType, ObjectTypeOwnership>
NSKeyValueCoding
source§impl<ObjectType: Message, ObjectTypeOwnership: Ownership> NSArray<ObjectType, ObjectTypeOwnership>
impl<ObjectType: Message, ObjectTypeOwnership: Ownership> NSArray<ObjectType, ObjectTypeOwnership>
NSKeyValueObserverRegistration
pub unsafe fn addObserver_toObjectsAtIndexes_forKeyPath_options_context(
&self,
observer: &NSObject,
indexes: &NSIndexSet,
keyPath: &NSString,
options: NSKeyValueObservingOptions,
context: *mut c_void
)
pub unsafe fn removeObserver_fromObjectsAtIndexes_forKeyPath_context(
&self,
observer: &NSObject,
indexes: &NSIndexSet,
keyPath: &NSString,
context: *mut c_void
)
pub unsafe fn removeObserver_fromObjectsAtIndexes_forKeyPath(
&self,
observer: &NSObject,
indexes: &NSIndexSet,
keyPath: &NSString
)
pub unsafe fn addObserver_forKeyPath_options_context(
&self,
observer: &NSObject,
keyPath: &NSString,
options: NSKeyValueObservingOptions,
context: *mut c_void
)
pub unsafe fn removeObserver_forKeyPath_context(
&self,
observer: &NSObject,
keyPath: &NSString,
context: *mut c_void
)
pub unsafe fn removeObserver_forKeyPath(
&self,
observer: &NSObject,
keyPath: &NSString
)
source§impl<ObjectType: Message, ObjectTypeOwnership: Ownership> NSArray<ObjectType, ObjectTypeOwnership>
impl<ObjectType: Message, ObjectTypeOwnership: Ownership> NSArray<ObjectType, ObjectTypeOwnership>
NSArrayPathExtensions
Methods from Deref<Target = NSObject>§
sourcepub fn is_kind_of<T>(&self) -> boolwhere
T: ClassType,
pub fn is_kind_of<T>(&self) -> boolwhere
T: ClassType,
Check if the object is an instance of the class, or one of it’s subclasses.
See Apple’s documentation for more details on what you may (and what you may not) do with this information.
Methods from Deref<Target = Object>§
sourcepub unsafe fn ivar_ptr<T>(&self, name: &str) -> *mut Twhere
T: Encode,
pub unsafe fn ivar_ptr<T>(&self, name: &str) -> *mut Twhere
T: Encode,
Returns a pointer to the instance variable / ivar with the given name.
This is similar to [UnsafeCell::get], see that for more information
on what is and isn’t safe to do.
Usually you will have defined the instance variable yourself with
ClassBuilder::add_ivar, the type of the ivar T must match the
type used in that.
Attempting to access or modify private implementation details of a class that you do no control using this is not supported, and may invoke undefined behaviour.
Library implementors are strongly encouraged to expose a safe interface to the ivar.
Panics
May panic if the object has no ivar with the given name. May also
panic if the type encoding of the ivar differs from the type encoding
of T.
This should purely seen as help while debugging and is not guaranteed
(e.g. it may be disabled when debug_assertions are off).
Safety
The object must have an instance variable with the given name, and it
must be of type T. Any invariants that the object have assumed about
the value of the instance variable must not be violated.
No thread syncronization is done on accesses to the variable, so you must ensure that any access to the returned pointer do not cause data races, and that Rust’s mutability rules are not otherwise violated.
sourcepub unsafe fn ivar<T>(&self, name: &str) -> &Twhere
T: Encode,
pub unsafe fn ivar<T>(&self, name: &str) -> &Twhere
T: Encode,
Returns a reference to the instance variable with the given name.
See Object::ivar_ptr for more information, including on when this
panics.
Safety
The object must have an instance variable with the given name, and it
must be of type T.
No thread syncronization is done, so you must ensure that no other
thread is concurrently mutating the variable. This requirement can be
considered upheld if all mutation happens through Object::ivar_mut
(since that takes &mut self).
sourcepub unsafe fn get_ivar<T>(&self, name: &str) -> &Twhere
T: Encode,
👎Deprecated: Use Object::ivar instead.
pub unsafe fn get_ivar<T>(&self, name: &str) -> &Twhere
T: Encode,
Object::ivar instead.sourcepub unsafe fn ivar_mut<T>(&mut self, name: &str) -> &mut Twhere
T: Encode,
pub unsafe fn ivar_mut<T>(&mut self, name: &str) -> &mut Twhere
T: Encode,
Returns a mutable reference to the ivar with the given name.
See Object::ivar_ptr for more information, including on when this
panics.
Safety
The object must have an instance variable with the given name, and it
must be of type T.
This access happens through &mut self, which means we know it to be
the only reference, hence you do not need to do any work to ensure
that data races do not happen.
sourcepub unsafe fn get_mut_ivar<T>(&mut self, name: &str) -> &mut Twhere
T: Encode,
👎Deprecated: Use Object::ivar_mut instead.
pub unsafe fn get_mut_ivar<T>(&mut self, name: &str) -> &mut Twhere
T: Encode,
Object::ivar_mut instead.sourcepub unsafe fn set_ivar<T>(&mut self, name: &str, value: T)where
T: Encode,
pub unsafe fn set_ivar<T>(&mut self, name: &str, value: T)where
T: Encode,
Sets the value of the ivar with the given name.
This is just a helpful shorthand for Object::ivar_mut, see that
for more information.
Safety
Same as Object::ivar_mut.
Trait Implementations§
source§impl<ObjectType: Message, ObjectTypeOwnership: Ownership> AsMut<NSArray<ObjectType, ObjectTypeOwnership>> for NSArray<ObjectType, ObjectTypeOwnership>
impl<ObjectType: Message, ObjectTypeOwnership: Ownership> AsMut<NSArray<ObjectType, ObjectTypeOwnership>> for NSArray<ObjectType, ObjectTypeOwnership>
source§impl<ObjectType: Message, ObjectTypeOwnership: Ownership> AsMut<NSArray<ObjectType, ObjectTypeOwnership>> for NSMutableArray<ObjectType, ObjectTypeOwnership>
impl<ObjectType: Message, ObjectTypeOwnership: Ownership> AsMut<NSArray<ObjectType, ObjectTypeOwnership>> for NSMutableArray<ObjectType, ObjectTypeOwnership>
source§impl<ObjectType: Message, ObjectTypeOwnership: Ownership> AsMut<NSObject> for NSArray<ObjectType, ObjectTypeOwnership>
impl<ObjectType: Message, ObjectTypeOwnership: Ownership> AsMut<NSObject> for NSArray<ObjectType, ObjectTypeOwnership>
source§impl<ObjectType: Message, ObjectTypeOwnership: Ownership> AsMut<Object> for NSArray<ObjectType, ObjectTypeOwnership>
impl<ObjectType: Message, ObjectTypeOwnership: Ownership> AsMut<Object> for NSArray<ObjectType, ObjectTypeOwnership>
source§impl<ObjectType: Message, ObjectTypeOwnership: Ownership> AsRef<NSArray<ObjectType, ObjectTypeOwnership>> for NSArray<ObjectType, ObjectTypeOwnership>
impl<ObjectType: Message, ObjectTypeOwnership: Ownership> AsRef<NSArray<ObjectType, ObjectTypeOwnership>> for NSArray<ObjectType, ObjectTypeOwnership>
source§impl<ObjectType: Message, ObjectTypeOwnership: Ownership> AsRef<NSArray<ObjectType, ObjectTypeOwnership>> for NSMutableArray<ObjectType, ObjectTypeOwnership>
impl<ObjectType: Message, ObjectTypeOwnership: Ownership> AsRef<NSArray<ObjectType, ObjectTypeOwnership>> for NSMutableArray<ObjectType, ObjectTypeOwnership>
source§impl<ObjectType: Message, ObjectTypeOwnership: Ownership> AsRef<NSObject> for NSArray<ObjectType, ObjectTypeOwnership>
impl<ObjectType: Message, ObjectTypeOwnership: Ownership> AsRef<NSObject> for NSArray<ObjectType, ObjectTypeOwnership>
source§impl<ObjectType: Message, ObjectTypeOwnership: Ownership> AsRef<Object> for NSArray<ObjectType, ObjectTypeOwnership>
impl<ObjectType: Message, ObjectTypeOwnership: Ownership> AsRef<Object> for NSArray<ObjectType, ObjectTypeOwnership>
source§impl<ObjectType: Message, ObjectTypeOwnership: Ownership> Borrow<NSArray<ObjectType, ObjectTypeOwnership>> for NSMutableArray<ObjectType, ObjectTypeOwnership>
impl<ObjectType: Message, ObjectTypeOwnership: Ownership> Borrow<NSArray<ObjectType, ObjectTypeOwnership>> for NSMutableArray<ObjectType, ObjectTypeOwnership>
source§impl<ObjectType: Message, ObjectTypeOwnership: Ownership> Borrow<NSObject> for NSArray<ObjectType, ObjectTypeOwnership>
impl<ObjectType: Message, ObjectTypeOwnership: Ownership> Borrow<NSObject> for NSArray<ObjectType, ObjectTypeOwnership>
source§impl<ObjectType: Message, ObjectTypeOwnership: Ownership> Borrow<Object> for NSArray<ObjectType, ObjectTypeOwnership>
impl<ObjectType: Message, ObjectTypeOwnership: Ownership> Borrow<Object> for NSArray<ObjectType, ObjectTypeOwnership>
source§impl<ObjectType: Message, ObjectTypeOwnership: Ownership> BorrowMut<NSArray<ObjectType, ObjectTypeOwnership>> for NSMutableArray<ObjectType, ObjectTypeOwnership>
impl<ObjectType: Message, ObjectTypeOwnership: Ownership> BorrowMut<NSArray<ObjectType, ObjectTypeOwnership>> for NSMutableArray<ObjectType, ObjectTypeOwnership>
source§fn borrow_mut(&mut self) -> &mut NSArray<ObjectType, ObjectTypeOwnership>
fn borrow_mut(&mut self) -> &mut NSArray<ObjectType, ObjectTypeOwnership>
source§impl<ObjectType: Message, ObjectTypeOwnership: Ownership> BorrowMut<NSObject> for NSArray<ObjectType, ObjectTypeOwnership>
impl<ObjectType: Message, ObjectTypeOwnership: Ownership> BorrowMut<NSObject> for NSArray<ObjectType, ObjectTypeOwnership>
source§fn borrow_mut(&mut self) -> &mut NSObject
fn borrow_mut(&mut self) -> &mut NSObject
source§impl<ObjectType: Message, ObjectTypeOwnership: Ownership> BorrowMut<Object> for NSArray<ObjectType, ObjectTypeOwnership>
impl<ObjectType: Message, ObjectTypeOwnership: Ownership> BorrowMut<Object> for NSArray<ObjectType, ObjectTypeOwnership>
source§fn borrow_mut(&mut self) -> &mut Object
fn borrow_mut(&mut self) -> &mut Object
source§impl<ObjectType: Message, ObjectTypeOwnership: Ownership> ClassType for NSArray<ObjectType, ObjectTypeOwnership>
impl<ObjectType: Message, ObjectTypeOwnership: Ownership> ClassType for NSArray<ObjectType, ObjectTypeOwnership>
source§impl<ObjectType: Message, ObjectTypeOwnership: Ownership> Deref for NSArray<ObjectType, ObjectTypeOwnership>
impl<ObjectType: Message, ObjectTypeOwnership: Ownership> Deref for NSArray<ObjectType, ObjectTypeOwnership>
source§impl<ObjectType: Message, ObjectTypeOwnership: Ownership> DerefMut for NSArray<ObjectType, ObjectTypeOwnership>
impl<ObjectType: Message, ObjectTypeOwnership: Ownership> DerefMut for NSArray<ObjectType, ObjectTypeOwnership>
source§impl<ObjectType: Hash + Message, ObjectTypeOwnership: Hash + Ownership> Hash for NSArray<ObjectType, ObjectTypeOwnership>
impl<ObjectType: Hash + Message, ObjectTypeOwnership: Hash + Ownership> Hash for NSArray<ObjectType, ObjectTypeOwnership>
This is implemented as a shallow copy.
As such, it is only possible when the array’s contents are Shared.
This is implemented as a shallow copy.