10 releases

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

#153 in macOS and iOS APIs

Download history 70908/week @ 2024-07-22 71939/week @ 2024-07-29 67704/week @ 2024-08-05 64013/week @ 2024-08-12 69989/week @ 2024-08-19 77644/week @ 2024-08-26 71662/week @ 2024-09-02 71080/week @ 2024-09-09 71564/week @ 2024-09-16 79500/week @ 2024-09-23 81211/week @ 2024-09-30 88875/week @ 2024-10-07 89245/week @ 2024-10-14 94937/week @ 2024-10-21 91428/week @ 2024-10-28 108121/week @ 2024-11-04

389,389 downloads per month
Used in 1,593 crates (116 directly)

MIT license

3MB
59K 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* Arc<[u8]>
NSMutableData* Vec<u8>
NSString* Arc<str>
NSMutableString* String
NSValue* Arc<dyn Any>
NSNumber* Arc<enum { I8(i8), U8(u8), I16(i16), U16(u16), I32(i32), U32(u32), I64(i64), U64(u64), F32(f32), F64(f64), CLong(ffi::c_long), CULong(ffi::c_ulong) }>
NSError* Arc<dyn Error + Send + Sync>
NSException* Arc<dyn Error + Send + Sync>
NSRange ops::Range<usize>
NSComparisonResult cmp::Ordering
NSArray<T>* Arc<[T]>
NSMutableArray<T>* Vec<T>
NSDictionary<K, V>* Arc<HashMap<K, V>>
NSMutableDictionary<K, V>* HashMap<K, V>
NSEnumerator<T>* Box<dyn Iterator<T>>
NSCopying* Box<dyn Clone>

Examples

Basic usage of a few Foundation types.

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

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

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

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

Dependencies