gpt4 book ai didi

ios - 类理解: Unexpected non-void return value in void function (swift 3)

转载 作者:行者123 更新时间:2023-11-30 12:59:39 24 4
gpt4 key购买 nike

我试图弄清楚类如何快速工作。我已经阅读了 dev.apple 上的类文档,但可能有一些基本内容我不明白。感谢您的帮助,如果可能的话,请澄清其工作原理。

运行 getData.connect 时,我无法从此函数返回 json 对象

错误:getData.swift:39:20:void 函数中出现意外的非 void 返回值

import UIKit
import Alamofire
import SwiftyJSON

public class getData: NSObject {
public func connect(url: String, devhostname: String, hostname: String, username: String, password: String) -> Array<JSON> {
let serverTrustPolicies: [String: ServerTrustPolicy] = [
hostname: .pinCertificates(
certificates: ServerTrustPolicy.certificates(),
validateCertificateChain: false,
validateHost: true
),
devhostname: .disableEvaluation
]

let sessionManager = Alamofire.SessionManager(
serverTrustPolicyManager: ServerTrustPolicyManager(policies: serverTrustPolicies)
)

var headers: HTTPHeaders = [:]

if let authorizationHeader = Request.authorizationHeader(user: username, password: password) {
headers[authorizationHeader.key] = authorizationHeader.value
}

sessionManager.request(url, headers: headers).responseJSON { response in

let data = response.data!
let json = JSON(data: data)

return json

sessionManager.session.invalidateAndCancel()
}
}
}

最佳答案

您的方法必须返回 Array<JSON>对象:

public func connect(url: String, devhostname: String, hostname: String, username: String, password: String) -> Array<JSON>

但是这段代码是异步执行的:

sessionManager.request(url, headers: headers).responseJSON { response in

let data = response.data!
let json = JSON(data: data)

return json

// this line is never executed too..
sessionManager.session.invalidateAndCancel()
}

因此,您的方法永远不会返回预期的数组。您必须从方法声明中删除 return 语句,并使用协议(protocol)/监听器或 CompletionHandler 管理异步响应。

关于ios - 类理解: Unexpected non-void return value in void function (swift 3),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39995351/

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