Expand description

Wraps NSUserDefaults, providing an interface to fetch and store small amounts of data.

In general, this tries to take an approach popularized by serde_json’s Value struct. In this case, Value handles wrapping types for insertion/retrieval, shepherding between the Objective-C runtime and your Rust code.

It currently supports a number of primitive types, as well as a generic Data type for custom usage. Note that the Data type is stored internally as an NSData instance.

Do not use this for storing sensitive data - you want the Keychain for that.

In general, you should expect that some allocations are happening under the hood here, due to the way the Objective-C runtime and Cocoa work. Where possible attempts are made to minimize them, but in general… well, profile the rest of your code first, and don’t call this stuff in a loop.

Example

use std::collections::HashMap;
use cacao::defaults::{UserDefaults, Value};

let mut defaults = UserDefaults::standard();

defaults.register({
    let mut map = HashMap::new();
    map.insert("test", Value::string("value"));
    map
});

let value = defaults.get("test").unwrap();
let value = value.as_str().unwrap();
assert_eq!(value, "value");

Structs

Wraps and provides methods for interacting with NSUserDefaults, which can be used for storing pieces of information (preferences, or defaults) to persist across application launches.

Enums

Represents a Value that can be stored or queried with UserDefaults.