Skip to content

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

Description

@Nazariglez

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!

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions