繁体   English   中英

在Objective C中调用Swift函数

[英]Call Swift function in Objective C

我有一个问题是使用我想在目标C中使用的Swift开发的方法。

Swift 4 Class:在这个函数中我检索用户的信息(比如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-Cm)我想在上面显示这些信息

#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"];
}];

但我得到这个错误信息没有可见@interface的'MySwiftClass'声明选择器'loadMemberProfil:'有人可以帮我吗? 请 :)

你需要

@objcMembers class MySwiftClass: UIViewController { --- }

要么

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

通过这个电话

[userProfil loadMemberProfilWithCompletion:^(NSDictionary*dic ) {

}];

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM