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

Creates an NSString from a static string.

Examples

This macro takes a either a "string" literal or const string slice as the argument, and produces a &'static NSString:

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

const WORLD: &str = "world";
let world = ns_string!(WORLD);
assert_eq!(world.to_string(), WORLD);

Unicode Strings

An NSString can contain strings with many different encodings, including ASCII, UTF-8, UTF-16, and so on. This macro automatically converts your string to the most efficient encoding, you don’t have to do anything!

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

Note that because this is implemented with const evaluation, massive strings can increase compile time, and may even hit the const evaluation limit.

NUL handling

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

let example = ns_string!("example\0");
assert_eq!(example.to_string(), "example\0");

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

Runtime Cost

None.

The result is equivalent to @"string" syntax in Objective-C.

Because of that, this should be preferred over NSString::from_str where possible.