UPGRADE YOUR SKILLS: Learn advanced Swift and SwiftUI on Hacking with Swift+! >>

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

2      

Hacking with Swift is sponsored by RevenueCat

SPONSORED Take the pain out of configuring and testing your paywalls. RevenueCat's Paywalls allow you to remotely configure your entire paywall view without any code changes or app updates.

Learn more here

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

Archived topic

This topic has been closed due to inactivity, so you can't reply. Please create a new topic if you need to.

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.