11 releases

0.3.0 Jan 22, 2025
0.2.2 May 21, 2024
0.2.0 Apr 17, 2024
0.2.0-alpha.6 Jul 19, 2022
0.2.0-alpha.2 Nov 22, 2021

#8 in macOS and iOS APIs

Download history 94997/week @ 2024-10-23 95151/week @ 2024-10-30 104434/week @ 2024-11-06 99419/week @ 2024-11-13 85965/week @ 2024-11-20 89507/week @ 2024-11-27 99412/week @ 2024-12-04 97730/week @ 2024-12-11 83931/week @ 2024-12-18 66435/week @ 2024-12-25 80905/week @ 2025-01-01 111339/week @ 2025-01-08 108588/week @ 2025-01-15 119976/week @ 2025-01-22 124894/week @ 2025-01-29 110405/week @ 2025-02-05

483,719 downloads per month
Used in 1,921 crates (165 directly)

MIT license

4.5MB
86K SLoC

objc2-foundation

Latest version License Documentation CI

Rust bindings to Apple's framework Foundation.

This README is kept intentionally small to consolidate the documentation, see the Rust docs for more details on this crate.

This crate is part of the objc2 project, see that for related crates.


lib.rs:

Bindings to the Foundation framework

See Apple's docs and the general docs on framework crates for more information.

This is the [std] equivalent for Objective-C, containing essential data types, collections, and operating-system services.

Rust vs. Objective-C types

A quick overview of some types you will encounter often in Objective-C, and their approximate Rust equivalent.

Objective-C (approximately) equivalent Rust
NSData* Rc<[u8]>
NSMutableData* Rc<Cell<Vec<u8>>>
NSString* Rc<str>
NSMutableString* Rc<Cell<String>>
NSValue* Rc<dyn Any>
NSNumber* Arc<enum { I8(i8), U8(u8), I16(i16), ... }>
NSError* Arc<dyn Error + Send + Sync>
NSException* Arc<dyn Error + Send + Sync>
NSRange ops::Range<usize>
NSComparisonResult cmp::Ordering
NSEnumerator<T>* Rc<dyn Iterator<Item = Retained<T>>>
NSCopying* Rc<dyn Clone>
NSArray<T>* Rc<[Retained<T>]>
NSMutableArray<T>* Rc<Cell<Vec<Retained<T>>>>
NSDictionary<K, V>* Rc<HashMap<Retained<K>, Retained<V>>>
NSMutableDictionary<K, V>* Rc<Cell<HashMap<Retained<K>, Retained<V>>>>

Note, in particular, that all "Mutable" variants use interior mutability, and that some things are thread-safe (Arc), while others are not (Rc).

Examples

Basic usage of a few Foundation types.

$ cargo add objc2-foundation
use objc2_foundation::{ns_string, NSCopying, NSArray};

let string = ns_string!("world");
println!("hello {string}");

let array = NSArray::from_slice(&[string]);
println!("{array:?}");

An example showing how to define your own interfaces to parts that may be missing in the autogenerated interface.

Dependencies