gpt4 book ai didi

ios - Xcode- swift 2.0 : "Extra argument ' error' in call"with NSJSONSerialization

转载 作者:行者123 更新时间:2023-11-28 15:48:10 25 4
gpt4 key购买 nike

<分区>

我正在开发一个具有登录屏幕的应用程序,这对我来说是第一次。在关注了为此提供教程的不同网站之后,我制作了一个 LoginService ViewController 来帮助登录应用程序。

这是我在这个 ViewController 中使用的部分代码:

 // MARK: Private Methods

private func exchangeTokenForUserAccessTokenWithCompletionHandler(username: String, password: String, completion: (OAuthInfo?, error: String?) -> ()) {

let path = "/oauthfake/token/"
let url = ConnectionSettings.apiURLWithPathComponents(path)
let request = NSMutableURLRequest(URL: url)
request.HTTPMethod = "POST"

var params = "client_id=\(ConnectionSettings.clientId)&client_secret=\(ConnectionSettings.clientSecret)&grant_type=password&login=\(username)&password=\(password)"

var err: NSError?
request.HTTPBody = params.dataUsingEncoding(NSUTF8StringEncoding, allowLossyConversion: false)
request.addValue("application/x-www-form-urlencoded", forHTTPHeaderField: "Content-Type")


let task = session.dataTaskWithRequest(request) {data, response, error -> Void in

var err: NSError?
var json = NSJSONSerialization.JSONObjectWithData(data, options: .MutableLeaves, error: &err) as? NSDictionary

if (err != nil) {

// Something went wrong, log the error to the console.
print(err!.localizedDescription)
let jsonStr = NSString(data: data!, encoding: NSUTF8StringEncoding)
print("Something went wrong: '\(jsonStr)")

completion(nil, error: err?.localizedDescription)
} else {
if let parseJSON = json {
if let token = parseJSON.valueForKey("access_token") as? String {
if var issuedAt = parseJSON.valueForKey("issued_at") as? String {
if var tokenExpiresIn = parseJSON.valueForKey("expires_in") as? String {
if var refreshTokenIssuedAt = parseJSON.valueForKey("refresh_token_issued_at") as? String {
if let refreshToken = parseJSON.valueForKey("refresh_token") as? String {
if var refreshTokenExpiresIn = parseJSON.valueForKey("refresh_token_expires_in") as? String {
if let refreshCount = parseJSON.valueForKey("refresh_count") as? String {

let epochIssuedAt:Double = (issuedAt as NSString).doubleValue / 1000.0
let epochRefreshTokenIssuedAt:Double = (refreshTokenIssuedAt as NSString).doubleValue / 1000.0

let oauthInfo = OAuthInfo(issuedAt: epochIssuedAt, refreshTokenIssuedAt: epochRefreshTokenIssuedAt, tokenExpiresIn: (tokenExpiresIn as NSString).doubleValue, refreshToken: refreshToken, token: token, refreshTokenExpiresIn: (refreshTokenExpiresIn as NSString).doubleValue, refreshCount: (refreshCount as NSString).integerValue)

completion(oauthInfo, error: err?.localizedDescription)

我的 super.init 似乎工作正常(上面没有提到),以及这个文件中的其余代码,但是我在这一行读到“Extra argument 'error' in call”时出错:

var json = NSJSONSerialization.JSONObjectWithData(data, options: .MutableLeaves, error: &err) as? NSDictionary

我已经了解到,Swift 2.0 处理错误的方式与我习惯的不同。我知道这个版本的 Swift 使用 try、let 和 catch 代码来检测错误。我的问题是我以前没有使用过任何这些类型的代码,并且不确定如何在这种特定情况下使用它们。

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