Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
984 views
in Technique[技术] by (71.8m points)

ios - Alamofire 5: Value of type 'Result<Data, AFError>' has no member 'value'

Is the a new error in Alamofire 5? as this wasn't running into bugs last time. Below are the code which are done. Anyone who used Alamofire facing this?

import Foundation
import Alamofire

class MyAppService {
static let shared = MyAppService()
let url = "http://127.0.0.1:5000"

private init() { }

func getCurrentUser(_ completion: @escaping (SomeRequest?) -> ()) {
    let path = "/somePath"
    AF.request("(url)(path)").responseData { response in
        if let data = response.result.value { //error shown here (Value of type 'Result<Data, AFError>' has no member 'value')
            let contact = try? SomeRequest(protobuf: data)
            completion(contact)
        }
        completion(nil)
    }
  }
}
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

You have to extract the result value as below,

func getCurrentUser(_ completion: @escaping (SomeRequest?) -> ()) {
    let path = "/somePath"
    AF.request("(url)(path)").responseData { response in
        switch response.result {
        case .success(let value):
            print(String(data: value, encoding: .utf8)!)
            completion(try? SomeRequest(protobuf: value))
        case .failure(let error):
            print(error)
            completion(nil)
        }
    }
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

2.1m questions

2.1m answers

60 comments

56.9k users

...