在没有 Alamofire 的更多上下文的情况下,表达类型是模棱两可的

问题描述 投票:0回答:0

我是 SwiftUI 的初学者,目前正在学习如何编码。一段时间以来,我一直在为代码中的错误而苦苦挣扎。这是我的代码和错误信息:

import SwiftUI
import Alamofire

struct LoginResponse: Codable {
    let success: Bool
    let result: LoginResult?
}

struct LoginResult: Codable {
    let appToken: String
    let trackId: Int
}

struct ContentView: View {
    
    @State var message: String = ""
    @State var appToken: String = ""
    @State var trackId: Int = 0
    
    var body: some View {
        VStack {
            Image(systemName: "person.circle")
                .imageScale(.large)
                .foregroundColor(.accentColor)
            
            Text("Freebox Login")
                .font(.title)
                .fontWeight(.bold)
            
            if message.isEmpty {
                Text("Loading...")
            } else {
                Text(message)
                if !appToken.isEmpty {
                    Text("App Token: \(appToken)")
                    Text("Track ID: \(trackId)")
                }
            }
        }
        .padding()
        .onAppear {
            let pathToCert = Bundle.main.path(forResource: "cert", ofType: "cer")!
            let localCertificate = NSData(contentsOfFile: pathToCert)!
            let certificates = [SecCertificateCreateWithData(nil, localCertificate)!]
            
            let pinnedEvaluator = PinnedCertificatesTrustEvaluator(certificates: certificates, acceptSelfSignedCertificates: true, performDefaultValidation: true, validateHost: true)
            
            let sessionManager: Session = Session(startRequestsImmediately: true, eventMonitors: [pinnedEvaluator])

            
            sessionManager.request("https://name.com/api/v4/login/authorize/", method: .post, parameters: ["app_id": "fr.freebox.name", "app_name": "name", "app_version": "0.1", "device_name": "iPhone"], encoding: JSONEncoding.default).responseDecodable(of: LoginResponse.self) { response in
                switch response.result {
                case .success(let loginResponse):
                    if loginResponse.success, let result = loginResponse.result {
                        self.appToken = result.appToken
                        self.trackId = result.trackId
                        self.message = "App Token and Track ID received!"
                    } else {
                        self.message = "Error: Login unsuccessful."
                    }
                case .failure(let error):
                    self.message = "Error: \(error.localizedDescription)"
                }
            }
        }
    }
}

struct ContentView_Previews: PreviewProvider {
    static var previews: some View {
        ContentView()
    }
}

这里是我的错误:

type of expression is ambiguous without more context

----------------------------------------

SchemeBuildError: Failed to build the scheme "Free Config"

type of expression is ambiguous without more context

Compile ContentView.swift (arm64):
/Users/julienbonzi/Documents/XCode/Free Config/Free Config/ContentView.swift:55:43: error: type of expression is ambiguous without more context
            let sessionManager: Session = Session(startRequestsImmediately: true, eventMonitors: [pinnedEvaluator])
                                          ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

我目前正在尝试在我的代码中使用 Alamofire 安装 SSL 证书,但我遇到了一个我一直在努力解决的错误。不幸的是,我似乎无法弄清楚是什么导致了这个问题。

你能帮我解决这个问题吗?

我尝试添加证书以将我的应用程序连接到 API 网站。

swift ssl-certificate alamofire
© www.soinside.com 2019 - 2024. All rights reserved.