Macro objc2_foundation::ns_string

source ·
macro_rules! ns_string {
    ($s:expr) => { ... };
}
Available on crate feature NSString only.
Expand description

Create a NSString from a static str.

Equivalent to the boxed C-strings @"string" syntax in Objective-C.

§Specification

The macro takes any expression that evaluates to a const &str, and produces a &'static NSString.

The returned string’s encoding is not guaranteed to be UTF-8.

Strings containing ASCII NUL is allowed, the NUL is preserved as one would expect.

§Cargo features

If the experimental "unstable-static-nsstring" feature is enabled, this will emit statics placed in special sections that will be replaced by dyld when the program starts up - which will in turn will cause the runtime cost of this macro to be completely non-existant!

However, it is known to not be completely reliable yet, see #258 for details.

§Examples

Creating a static NSString.

use objc2_foundation::{ns_string, NSString};

let hello: &'static NSString = ns_string!("hello");
assert_eq!(hello.to_string(), "hello");

Creating a NSString from a const &str.

const EXAMPLE: &str = "example";
assert_eq!(ns_string!(EXAMPLE).to_string(), EXAMPLE);

Creating unicode strings.

let hello_ru = ns_string!("Привет");
assert_eq!(hello_ru.to_string(), "Привет");

Creating a string containing a NUL byte:

assert_eq!(ns_string!("example\0").to_string(), "example\0");
assert_eq!(ns_string!("exa\0mple").to_string(), "exa\0mple");