Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Basic documentation code does not compile #119

Closed
mdenty opened this issue Jan 12, 2023 · 2 comments
Closed

Basic documentation code does not compile #119

mdenty opened this issue Jan 12, 2023 · 2 comments

Comments

@mdenty
Copy link

mdenty commented Jan 12, 2023

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
Copy link

notgull commented Jan 12, 2023

Add this to lib.rs:

#[macro_use]
extern crate objc;

@mdenty
Copy link
Author

mdenty commented Jan 12, 2023

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}");
    }
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants