WWDC22 SALE: Save 50% on all my Swift books and bundles! >>

Objective-C and Swift Mixed Compile

Forums > Swift

Is there have any easy waty to get the property value from Swift to Objective-C ? In my case First I will declare a struct In swift like this

struct studentBean: Codable { let name: String }

Second I hope I can get the message after network done, so I create an closure to fetch student message like this func loadStudentInfo(onSuccess: @escaping (studentBean) -> Void) { //NetWork Request }

Third I have to exposed this message to Objective-C ,In Student.swift I have to declare three times about student name and make sure oc can use it @objc public class current class inherited from NSObject that OC have access permissions

@objc public class studentModel: NSObject { init(bean: studentBean) { name = bean.name } @objc public let name: String }

Eventually in Objective-C use the swift function

[Student loadStudentInfo:^(studentModel * Nonnull model){ label.text = model.name; }];

This calling chain make too long too complicated


Hacking with Swift is sponsored by Emerge

SPONSORED Why are Swift reference types bad for app startup time, and what’s the performance cost of protocol conformances? That’s just a couple of the topics you can learn about on the Emerge blog — written by the app performance experts behind Emerge’s advanced app optimization and monitoring tools, based on their experience of working at companies like Apple, Airbnb, Snap, and Spotify.

Find out more

Sponsor Hacking with Swift and reach the world's largest Swift community!

Reply to this topic…

You need to create an account or log in to reply.

All interactions here are governed by our code of conduct.

Unknown user

You are not logged in

Log in or create account

Link copied to your pasteboard.