gpt4 book ai didi

ios - 在 Objective C 中调用 Swift 函数

转载 作者:搜寻专家 更新时间:2023-11-01 06:27:05 24 4
gpt4 key购买 nike

我在使用 Swift 开发的方法时遇到问题,我想在 Objective-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"];
}];

但我收到此错误消息 No visible @interface for 'MySwiftClass' declares the selector 'loadMemberProfil:' 有人可以帮我吗?请 :)

最佳答案

你需要

@objcMembers class MySwiftClass: UIViewController { --- }

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

通过这个电话

[userProfil loadMemberProfilWithCompletion:^(NSDictionary*dic ) {

}];

关于ios - 在 Objective C 中调用 Swift 函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52972621/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com