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

iOS ClassDecl panic - Should I do something before a ClassDecl? #113

Closed
Nazariglez opened this issue May 29, 2022 · 3 comments
Closed

iOS ClassDecl panic - Should I do something before a ClassDecl? #113

Nazariglez opened this issue May 29, 2022 · 3 comments

Comments

@Nazariglez
Copy link

Nazariglez commented May 29, 2022

Hey hi!

I'm trying to do something simple, just declaring my AppDelegate like this lib.rs:

use objc::declare::ClassDecl;
use objc::runtime::{BOOL, Object, Sel, YES, NO};

type Obj = *mut Object;

extern "C" fn did_finish_launching_with_options(_: &mut Object, _: Sel, _: Obj, _: Obj) -> BOOL {
    println!("DID FINISH LAUNCHING");
    YES
}

pub fn start() {
    println!("START - HELLO FROM LIB?");


    let ui_responder_cls = class!(UIResponder);
    let mut app_delegate_cls = ClassDecl::new("AppDelegate", ui_responder_cls)
        .expect("Failed to declare AppDelegate");

    unsafe {
        app_delegate_cls.add_method(
            sel!(application:didFinishLaunchingWithOptions:),
            did_finish_launching_with_options as extern "C" fn(&mut Object, Sel, Obj, Obj) -> BOOL,
        );

        app_delegate_cls.register();
    }
}

#[no_mangle]
pub extern "C" fn start_app() {
    start();
}

And it's used on xcode as this main.m:

#import "ffi.h"

int main(int argc, char * argv[]) {
        start_app();
        return 0;
}

But always got a panic when I do the expect, is an Option so I don't have an error to read and to know what was wrong. I checked the examples, and reduced the examples to the minimum, using NSObject and extending it instead of using UIResponder, and it's the same result.

Am I missing something? (Testing on simulator).

Thanks!

@madsmtm
Copy link

madsmtm commented May 29, 2022

The class name has to be unique, there probably exists a class named AppDelegate somewhere already. Try MyAppDelegate, MyAppDelegate1, MyRustAppDelegate or similar names.

@Nazariglez
Copy link
Author

Nazariglez commented May 30, 2022

This is it. My fault. I was trying to reproduce the same code that I had on my xcode project from rust. Does make sense to change the option for an error telling the user that the name should be unique?

I may be dumb, however, I think that some extra info will be nice.

Thanks!

@madsmtm
Copy link

madsmtm commented May 31, 2022

Does make sense to change the option for an error telling the user that the name should be unique?

Probably would be a good idea, or at the very least document duplicate names as a common error case.

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