Unable to use Swift object's variables in Objective-C View Controller

Multi tool use
Multi tool use


Unable to use Swift object's variables in Objective-C View Controller



I am facing a problem accessing variables of a class written in Swift from a view controller written in Objective-C.



I have already created the Bridging Header and I have successfully sent the Swift object from a view controller written in Swift to another one written in Objective-C. The problem is that I want to access the variables of the object but I get the following error: Property 'variableName' cannot be found in forward class object 'className'.



Here is the class which I am trying to access its variables:


import Foundation
import UIKit
import SwiftyJSON

@objcMembers
class SwiftObject: NSObject {

// MARK: - Variables
var id: String
var name: String
var contact: SwiftSubObjectA
var location: SwiftSubObjectB
var categories: [SwiftSubObjectC] =

// MARK: - Initializers
init(withJSON json: JSON) {
self.id = json["id"].stringValue
self.name = json["name"].stringValue
self.contact = SwiftSubObjectA(withJSON: json["contact"])
self.location = SwiftSubObjectB(withJSON: json["location"])

for categoryJSON in json["categories"].arrayValue {
categories.append(SwiftSubObjectC(withJSON: categoryJSON))
}

}

}



And this is how I am sending the object into the Objective-C view controller


class SwiftViewController: UIViewController {

override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
guard let objectiveVC.object = segue.destination as? ObjectiveViewController else { return }
guard let object = sender as? SwiftObject else { return }
objectiveVC.object = object
}

}



Finally, this is the Objective-C view controller .h file:


#import <UIKit/UIKit.h>
@class SwiftObject;

@interface ObjectiveViewController : UIViewController

@property (strong, nonatomic) SwiftObject * _Nonnull object;

@end



and the .m file:


#import "ObjectiveViewController.h"
#import "Project-Bridging-Header.h"

@interface ObjectiveViewController ()

@end

@implementation ObjectiveViewController

- (void)viewDidLoad {
[super viewDidLoad];
NSLog(@"%@", self.object.name);
// Do any additional setup after loading the view.
}

@end



The problem is that the self.object does not have any available variables to be used.




1 Answer
1



In order to access Swift object in your Objective-C Class you need to import Project-Swift.hin your .m file as shown below


Project-Swift.h


#import "Project-Swift.h"

@implementation ObjectiveViewController

//... other code

@end



Project is the name of the project. Add the import statement in your ObjectiveViewController.m file


ObjectiveViewController.m



Hope this helps





Thank you!! I had applied it but I deleted it by mistake.
– PV169
Jul 1 at 17:35





@pv169 : Am glad you found the solution :)
– Sandeep Bhandari
Jul 1 at 17:37






By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

ZbPXdv7xkDROS2vD RS,UKN Sjo,Xy,Z g M9AjuVzbrUTqBwFdcoe7J4Bm4Xv8fFSYEPyfYK2YPfRVMlLOz7mwD
d9zfsaUFjrdZfOgePdED4cBB QHKJ,KJ8K37cA3jA02,9FWs Z1nn 1X,UyHgM4xuZfkWh0LjcBcHOGMHd85,gSjx0hJ,t

Popular posts from this blog

Rothschild family

Boo (programming language)