SSheldon / rust-objc

Objective-C Runtime bindings and wrapper for Rust.
https://crates.io/crates/objc
MIT License
393 stars 57 forks source link

How do I call a class function? #92

Closed LoganDark closed 4 years ago

LoganDark commented 4 years ago

I'm trying to call NSFont.boldSystemFont, but I can't figure out how. This doesn't work (size is a *mut NSObject):

let font: *mut Object = msg_send![class!(NSFont), boldSystemFont:size];

Or this:

let font: *mut Object = msg_send![class!(NSFont), boldSystemFont(size)];

Or this:

let font: *mut Object = msg_send![class!(NSFont), boldSystemFont, ofSize:size];

Or this:

let font: *mut Object = msg_send![class!(NSFont), boldSystemFont:, ofSize:size];

Or this:

let font: *mut Object = msg_send![class!(NSFont), boldSystemFont:ofSize:size];

Or this:

let font: *mut Object = msg_send![class!(NSFont), ofSize:size];

Or this:

let nsfont = class!(NSFont);
let method = nsfont.instance_method(sel!(boldSystemFont)).unwrap();
let font: *mut Object = msg_send![method, ofSize:size];

Or this:

let method = sel!(NSFont.boldSystemFont);
let font: *mut Object = msg_send![method, ofSize:size];

Or this:

let class = class!(NSFont);
let method = sel!(boldSystemFont:);
let font: *mut Object = class.send_message(method, size).unwrap();

I feel like I've tried everything, but this is just so basic that objc MUST have functionality for it somewhere... right?

Please help me, I've spent way too much time trying to figure this out... That's not even everything I tried, but I don't want to make this issue any longer!

SSheldon commented 4 years ago

@LoganDark you need to switch that docs page to the Objective-C language: https://developer.apple.com/documentation/appkit/nsfont/1533549-boldsystemfontofsize?language=objc

Then you'll see that the method name in Objective-C is boldSystemFontOfSize:. So the code you'd want is something like:

let font: *mut Object = msg_send![class!(NSFont), boldSystemFontOfSize:size];
LoganDark commented 4 years ago

Oh, thank you! :D

SSheldon commented 4 years ago

size is a *mut NSObject

Also, unless that was a typo, you'll see in the method signature that size should be a CGFloat rather than an object.

LoganDark commented 4 years ago

Huh, I assumed all things in Objective-C were objects... this is my first day D:

objc doesn't provide a CGFloat type, is there an equivalent in Rust (like is it just an f32?) or anything?

Also where should I go in the future to get help on this? I don't like having to bother the repository owner for help, you probably have way better things to do :p

Edit: Seems like specifying f32 or f64 as the return type both work... the docs say it's a double on 64-bit architectures so I guess I'm gonna do f64.