SSheldon / rust-objc

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

Basic documentation code does not compile #119

Closed mdenty closed 1 year ago

mdenty commented 1 year ago
use objc::{class, msg_send};
use objc::runtime::{BOOL, Object};

fn main() {
    println!("Hello, world!");
    let cls = class!(NSObject);
    let obj: *mut Object = msg_send![cls, new];
    let hash: usize = msg_send![obj, hash];
    let is_kind: BOOL = msg_send![obj, isKindOfClass:cls];
// Even void methods must have their return type annotated
    let _: () = msg_send![obj, release];
}

The errors are :

error: cannot find macro `sel` in this scope
 --> src/main.rs:7:28
  |
7 |     let obj: *mut Object = msg_send![cls, new];
  |                            ^^^^^^^^^^^^^^^^^^^
  |
  = note: this error originates in the macro `msg_send` (in Nightly builds, run with -Z macro-backtrace for more info)

error: cannot find macro `sel` in this scope
 --> src/main.rs:8:23
  |
8 |     let hash: usize = msg_send![obj, hash];
  |                       ^^^^^^^^^^^^^^^^^^^^
  |
  = note: this error originates in the macro `msg_send` (in Nightly builds, run with -Z macro-backtrace for more info)

error: cannot find macro `sel` in this scope
 --> src/main.rs:9:25
  |
9 |     let is_kind: BOOL = msg_send![obj, isKindOfClass:cls];
  |                         ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  |
  = note: this error originates in the macro `msg_send` (in Nightly builds, run with -Z macro-backtrace for more info)

error: cannot find macro `sel` in this scope
  --> src/main.rs:11:17
   |
11 |     let _: () = msg_send![obj, release];
   |                 ^^^^^^^^^^^^^^^^^^^^^^^
   |
   = note: this error originates in the macro `msg_send` (in Nightly builds, run with -Z macro-backtrace for more info)

error: could not compile `objc-test` due to 4 previous errors
notgull commented 1 year ago

Add this to lib.rs:

#[macro_use]
extern crate objc;
mdenty commented 1 year ago

Thank you very much for the quick answer. I suppose this should be in the documentation of the base example to avoid such basic question of noobs like me.

The working code in my main.rs is

#[macro_use]
extern crate objc;

use objc::{class, msg_send};
use objc::runtime::{BOOL, Object};

fn main() {
    println!("Hello, world!");
    unsafe {
        let cls = class!(NSObject);
        let obj: *mut Object = msg_send![cls, new];
        let hash: usize = msg_send![obj, hash];
        let is_kind: BOOL = msg_send![obj, isKindOfClass:cls];
    // Even void methods must have their return type annotated
        let _: () = msg_send![obj, release];
        println!("{hash}: {is_kind}");
    }
}