Struct icrate::Foundation::NSArray

source ·
#[repr(C)]
pub struct NSArray<ObjectType: Message = Object, ObjectTypeOwnership: Ownership = Shared> { /* private fields */ }
Available on crate feature Foundation only.

Implementations§

Generic creation methods.

Get an empty array.

Examples found in repository?
src/Foundation/additions/array.rs (line 260)
259
260
261
    fn default_id() -> Id<Self, Self::Ownership> {
        Self::new()
    }
Examples found in repository?
examples/basic_usage.rs (line 18)
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.

Examples found in repository?
examples/nspasteboard.rs (line 79)
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");
        }
    }

Generic accessor methods.

Examples found in repository?
examples/basic_usage.rs (line 22)
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
Hide additional examples
src/Foundation/additions/array.rs (line 102)
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
            }
        }
src/Foundation/additions/mutable_array.rs (line 51)
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
                );
            }
        }
Examples found in repository?
src/Foundation/additions/array.rs (line 172)
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
Hide additional examples
src/Foundation/additions/mutable_array.rs (line 67)
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()
    }
Examples found in repository?
examples/nspasteboard.rs (line 84)
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()
    }
Examples found in repository?
src/Foundation/additions/mutable_array.rs (line 89)
88
89
90
91
92
93
94
95
96
        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
                })
        }
Examples found in repository?
examples/basic_usage.rs (line 19)
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());
}
Examples found in repository?
src/Foundation/additions/array.rs (line 161)
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()
        }
Examples found in repository?
examples/basic_usage.rs (line 25)
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.

Accessor methods that work on owned arrays.

Examples found in repository?
src/Foundation/additions/array.rs (line 251)
250
251
252
    fn index_mut(&mut self, index: usize) -> &mut T {
        self.get_mut(index).unwrap()
    }
More examples
Hide additional examples
src/Foundation/additions/mutable_array.rs (line 182)
181
182
183
    fn index_mut(&mut self, index: usize) -> &mut T {
        self.get_mut(index).unwrap()
    }

NSArrayCreation

NSDeprecated

Examples found in repository?
src/Foundation/additions/array.rs (line 98)
97
98
99
        pub fn len(&self) -> usize {
            self.count()
        }

NSExtendedArray

Examples found in repository?
src/Foundation/additions/array.rs (line 139)
135
136
137
138
139
140
141
142
143
        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)
            }
        }

NSArrayCreation

NSArrayDiffing

NSDeprecated

NSKeyValueCoding

NSKeyValueObserverRegistration

NSArrayPathExtensions

NSPredicateSupport

NSSortDescriptorSorting

Methods from Deref<Target = NSObject>§

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>§

Dynamically find the class of this object.

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.

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).

👎Deprecated: Use Object::ivar instead.

Use Object::ivar instead.

Safety

See Object::ivar.

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.

👎Deprecated: Use Object::ivar_mut instead.

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§

Converts this type into a mutable reference of the (usually inferred) input type.
Converts this type into a mutable reference of the (usually inferred) input type.
Converts this type into a mutable reference of the (usually inferred) input type.
Converts this type into a mutable reference of the (usually inferred) input type.
Converts this type into a shared reference of the (usually inferred) input type.
Converts this type into a shared reference of the (usually inferred) input type.
Converts this type into a shared reference of the (usually inferred) input type.
Converts this type into a shared reference of the (usually inferred) input type.
Immutably borrows from an owned value. Read more
Immutably borrows from an owned value. Read more
Immutably borrows from an owned value. Read more
Mutably borrows from an owned value. Read more
Mutably borrows from an owned value. Read more
Mutably borrows from an owned value. Read more
The superclass of this class. Read more
The name of the Objective-C class that this type represents.
Get a reference to the Objective-C class that this type represents. Read more
Get an immutable reference to the superclass.
Get a mutable reference to the superclass.
Allocate a new instance of the class. Read more
Formats the value using the given formatter. Read more
Indicates whether the default value is mutable or immutable.
The default Id for a type. Read more
The resulting type after dereferencing.
Dereferences the value.
Mutably dereferences the value.
Feeds this value into the given Hasher. Read more
Feeds a slice of this type into the given Hasher. Read more
The returned type after indexing.
Performs the indexing (container[index]) operation. Read more
Performs the mutable indexing (container[index]) operation. Read more
The type of the elements being iterated over.
Which kind of iterator are we turning this into?
Creates an iterator from a value. Read more

This is implemented as a shallow copy.

As such, it is only possible when the array’s contents are Shared.

Indicates whether the type is mutable or immutable. Read more
The output type. Read more

This is implemented as a shallow copy.

The output type. Read more
This method tests for self and other values to be equal, and is used by ==.
This method tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
The Objective-C type-encoding for a reference of this type. Read more
The resulting type after obtaining ownership.
Creates owned data from borrowed data, usually by cloning. Read more
Uses borrowed data to replace owned data, usually by cloning. Read more

Auto Trait Implementations§

Blanket Implementations§

Gets the TypeId of self. Read more
Immutably borrows from an owned value. Read more
Mutably borrows from an owned value. Read more

Returns the argument unchanged.

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

The type returned in the event of a conversion error.
Performs the conversion.
The type returned in the event of a conversion error.
Performs the conversion.