Alamofire Fire Variable Type Has No Subscript Members

Type 'Any' has no subscript members error on Alamofire 4.0

Try below code:

Alamofire.request(url!, method: .get, parameters: nil, encoding: JSONEncoding.default, headers: nil).responseJSON { response in

switch(response.result) {
case .success(_):
if let JSON = response.result.value as! [[String : Any]]!{
print("JSON: \(JSON)")
let dic = JSON[0] as [String:AnyObject]!
print("TitularEmail : ",dic?["TitularEmail"])
}
break

case .failure(_):
print("There is an error")
break
}
}

Type Any has no subscript members / Swift 3

You need to specify the type of your JSON object to [String : Any].

 if let JSON = response.result.value  as?  [String : Any] {
if let items = JSON["items"] as? [[String : Any]] {
for video in items {
//Here use video["snippet.resourceId.videoId"] instead of value for key
}
}
}

Note : In swift it is batter if you use swift generic Array and dictionary objects instead of NSArray & NSDictionary.

swift 3 Type 'Any' has no subscript members?

Typecast response as:

if let json = response.result.value as? [String:AnyObject]{..}

Alamofire 5: Value of type 'ResultData, AFError' has no member 'value'

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)
}
}
}


Related Topics



Leave a reply



Submit