madsmtm / objc2

Bindings to Apple's frameworks in Rust
https://docs.rs/objc2/
MIT License
280 stars 35 forks source link

how to send nil #598

Closed eks5115 closed 3 weeks ago

eks5115 commented 1 month ago

how to do send nil ?

example: msg_send![&obj, param1: value, param2: nil]

madsmtm commented 1 month ago

It kinda depends on the method you're calling; if you can use a generated method in icrate, you should do that, then you can just pass None to methods that expect nil.

If what you need is not in icrate, then I suggest you make a helper function for your msg_send! call, so that you can ensure that the types are correct, something like the following (again, exact signature depends on the method you're calling):

fn call_with_param1_param2(obj: &NSObject, param1: MyParam, param2: Option<&NSObject>) {
    unsafe { msg_send![obj, param1: param1, param2: param2] }
}

call_with_param1_param2(&obj, value, None);

If you want to avoid all of this, you can use the following to get the a NULL pointer that you can use in message sends.

use std::ptr;
use objc2::runtime::AnyObject;

let nil: *const AnyObject = ptr::null();
eks5115 commented 3 weeks ago

Thank you very much. All of these methods will work