简体   繁体   中英

Custom init with id to ViewController in Objective-C from Swift code

I need help you with convert my Swift code to Objective-c.

There is my Swift code.

 self.show(ChatViewController(conversation: conversation!), sender: self)

I have no idea how to convert them to with init "conversation". This is Id of conversation for my chat in Socket.

 ChatViewController *chatVC = [[ChatViewController alloc] init];
            [vc showViewController: chatVC sender:self];

All Swift stuff must be marked @objc public to be visible in Objective-C. In this case that would be the ChatViewController class, its init(conversation:) , and whatever the conversation type is.

If you do that correctly, Objective-C should be able to import your generated interface header (find this in your build settings, it usually looks like "AppName-Swift.h").

Then the Swift methods would be automatically converted to Objective-C style methods so you would be able to call

ChatViewController *chatVC = [[ChatViewController alloc] initWithConversation:converstaion];

May be Swift can't bridge your conversation object

It is not clear what conversation type is, but swift constructor

ChatViewController(conversation: conversation!)

in Objective-C is

ChatViewController *chatVC = [[ChatViewController alloc] 
    initWithConversation:conversation];

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM