GeekThuG
GeekThuG

Reputation: 25

Call Swift function in Objective C

I have a problem to use a method developed in Swift that I want to use in objective C.

Swift 4 Class: in this function I retrieve the information of the user (like userNumber, secretCode)

@objc class MySwiftClass: HPTableViewController {
    func loadMemberProfil(completion: ((_ sub : [String: AnyObject]) -> Void)!) {
        //API get profile and Bearer token
        let token = HPWSLoginManager.shared().saveSuccessResponse.token
        let url = URL(string: "http://51.38.36.76:40/api/v1/profile")
        var request = URLRequest(url: url!)
        request.httpMethod = "GET"
        request.addValue("Bearer \(token!)", forHTTPHeaderField: "Authorization")
        //get information in token
        URLSession.shared.dataTask(with: request) { (data, response, error) in
            guard let data = data else { return }
            do {
                let json = try JSONSerialization.jsonObject(with: data, options: .mutableContainers) as! [String: AnyObject]
                let sub = json["sub"] as! [String: AnyObject]
                if completion != nil{
                    completion(sub)
                }
            } catch {
                print("error")
            }
            }.resume()
    }
}

for example at the end of the function I get

  sub["usernumber"]! // print +224625259239
  sub["secretcode"]! // print $2a$08$DIrq1iKnjkkY4KgI3Mqy7.aWC39m2aFMncfJSkom9l0yaXhGlH35m

Objective-C implementation (Objective-C.m) I want to display this information above

#import "ProjectName-Swift.h"
MySwiftClass* userProfil = [[MySwiftClass alloc]init];
[userProfil  loadMemberProfil: ^(NSDictionary *sub) { // No visible @interface for 'MySwiftClass' declares the selector 'loadMemberProfil:'
userNumber = sub[@"usernumber"];
secretCode = sub[@"secretcode"];
}];

but I get this error message No visible @interface for 'MySwiftClass' declares the selector 'loadMemberProfil:' could someone help me? please :)

Upvotes: 1

Views: 156

Answers (1)

Shehata Gamal
Shehata Gamal

Reputation: 100543

You need

@objcMembers class MySwiftClass: UIViewController { --- }

OR

@objc func loadMemberProfil(completion: ((_ sub : [String: AnyObject]) -> Void)!) { --- }

With this call

[userProfil loadMemberProfilWithCompletion:^(NSDictionary*dic ) {

}];

Upvotes: 3

Related Questions