iOS:如何在 objective C 中使用我的 swift class 中的特定功能? `使用未声明的标识符 'NetworkClient'`
iOS: How to user a specific function from my swift class in objective C? `Use of undeclared identifier 'NetworkClient'`
我在 Swift 中使用此代码效果很好,但无法在 Objective C 中调用它。
import Foundation
import Alamofire
enum Router: URLRequestConvertible {
case users
static let baseURLString = "https://api.stackexchange.com/2.2"
func asURLRequest() throws -> URLRequest {
let path: String
switch self {
case .users:
path = "/users?order=desc&sort=reputation&site=Whosebug&max=10"
}
let url = URL(string: Router.baseURLString + path)!
return URLRequest(url: url)
}
}
@objcMembers public final class NetworkClient {
// 1
let evaluators = [
"api.stackexchange.com":
PinnedCertificatesTrustEvaluator(certificates: [
Certificates.stackExchange
])
]
let session: Session
// 2
private init() {
session = Session(
serverTrustManager: ServerTrustManager(evaluators: evaluators)
)
} ...
我在Swift下是这样使用的:
NetworkClient.request(Router.users)
.responseDecodable { (response: DataResponse<UserList>) in
switch response.result {
case .success(let value):
self.users = value.users
case .failure(let error):
let isServerTrustEvaluationError =
error.asAFError?.isServerTrustEvaluationError ?? false
let message: String
if isServerTrustEvaluationError {
message = "Certificate Pinning Error"
} else {
message = error.localizedDescription
}
self.presentError(withTitle: "Oops!", message: message)
}
}
我在 Objective C 下尝试过但没有成功:
NetworkClient.request.responseDecodable()
{
}
错误信息是:Use of undeclared identifier 'NetworkClient'
- 我正确添加了 #import myapp-Swift.h
- 我添加了@objcMembers,但我不知道这样做是否正确。
有什么想法吗?提前致谢。
您的 Swift class 需要从 NSObject
继承才能对 Objective-C.
可见
public final class NetworkClient: NSObject {
// ...
}
我在 Swift 中使用此代码效果很好,但无法在 Objective C 中调用它。
import Foundation
import Alamofire
enum Router: URLRequestConvertible {
case users
static let baseURLString = "https://api.stackexchange.com/2.2"
func asURLRequest() throws -> URLRequest {
let path: String
switch self {
case .users:
path = "/users?order=desc&sort=reputation&site=Whosebug&max=10"
}
let url = URL(string: Router.baseURLString + path)!
return URLRequest(url: url)
}
}
@objcMembers public final class NetworkClient {
// 1
let evaluators = [
"api.stackexchange.com":
PinnedCertificatesTrustEvaluator(certificates: [
Certificates.stackExchange
])
]
let session: Session
// 2
private init() {
session = Session(
serverTrustManager: ServerTrustManager(evaluators: evaluators)
)
} ...
我在Swift下是这样使用的:
NetworkClient.request(Router.users)
.responseDecodable { (response: DataResponse<UserList>) in
switch response.result {
case .success(let value):
self.users = value.users
case .failure(let error):
let isServerTrustEvaluationError =
error.asAFError?.isServerTrustEvaluationError ?? false
let message: String
if isServerTrustEvaluationError {
message = "Certificate Pinning Error"
} else {
message = error.localizedDescription
}
self.presentError(withTitle: "Oops!", message: message)
}
}
我在 Objective C 下尝试过但没有成功:
NetworkClient.request.responseDecodable()
{
}
错误信息是:Use of undeclared identifier 'NetworkClient'
- 我正确添加了 #import myapp-Swift.h
- 我添加了@objcMembers,但我不知道这样做是否正确。
有什么想法吗?提前致谢。
您的 Swift class 需要从 NSObject
继承才能对 Objective-C.
public final class NetworkClient: NSObject {
// ...
}