Skip to content

How to import and use constants #114

@betamos

Description

@betamos

I'm modifying a library that uses objc. I need to import a constant NSString. I tried const* NSString and Id<NSString> but rustc warned me that these are not "FFI safe". This, however, appears to work:

// required to bring NSPasteboard into the path of the class-resolver
#[link(name = "AppKit", kind = "framework")]
extern "C" {
    // NSString
    static NSPasteboardURLReadingFileURLsOnlyKey: &'static Object;
}

Here is the usage:

let ns_dict = class!(NSDictionary);
let ns_number = class!(NSNumber);

let options: Id<NSDictionary<NSObject, NSObject>> = unsafe {
    let obj: Id<NSObject> = msg_send![ns_number, numberWithBool: objc::runtime::YES];
    msg_send![ns_dict, dictionaryWithObject:obj forKey: NSPasteboardURLReadingFileURLsOnlyKey]
};

Is this correct, safe and idiomatic?

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions