在Objective C中调用Swift函数

时间:2018-10-24 15:19:28

标签: ios objective-c swift xcode

使用在Swift中开发的方法要在目标C中使用时遇到问题。

Swift 4类:在此函数中,我检索用户的信息(例如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()
    }
}

例如在我得到的函数的结尾

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

Objective-C实现(Objective-C.m)我想在

上方显示此信息
#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"];
}];

但我收到此错误消息'MySwiftClass'的无可见@interface声明选择器'loadMemberProfil:'有人可以帮助我吗?请:)

1 个答案:

答案 0 :(得分:3)

您需要

@objcMembers class MySwiftClass: UIViewController { --- }

OR

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

通过此通话

[userProfil loadMemberProfilWithCompletion:^(NSDictionary*dic ) {

}];