Alamofire 类型的表达式在没有更多上下文的情况下是模棱两可的
Posted
技术标签:
【中文标题】Alamofire 类型的表达式在没有更多上下文的情况下是模棱两可的【英文标题】:Alamofire type of expression is ambiguous without more context 【发布时间】:2015-11-22 15:08:57 【问题描述】:我知道同一主题有多个问题,尽管我的问题与这些问题之间的区别似乎是定义参数。更奇怪的是代码会在我的 iPhone 上构建并安装到昨天。
这就是 Xcode 抱怨的地方
private class func getLocItemsAtPath(path: String!, completionHandler: (LocItemsWrapper?, NSError?) -> Void)
Alamofire.request(.GET, path!)
.responseLocItemsArray response in
if let error = response.result.error
completionHandler(nil, error)
return
completionHandler(response.result.value, nil)
这是整个班级
import Foundation
import Alamofire
import SwiftyJSON
enum LocItemFields: String
case Name = "name"
case LocationBackground = "locationBackground"
case Logo = "logo"
case Status = "status"
case Company = "company"
case Id = "id"
case Url = "url"
class LocItemsWrapper
var locItems: Array<LocItems>?
var count: Int?
private var next: String?
private var previous: String?
class LocItems
var idNumber: Int?
var name: String?
var locationBackground: String?
var logo: String?
var status: String?
var company: String?
var id: String?
var url: String?
required init(json: JSON, id: Int?)
//print(json)
self.idNumber = id
self.name = json[LocItemFields.Name.rawValue].stringValue
self.locationBackground = json[LocItemFields.LocationBackground.rawValue].stringValue
self.logo = json[LocItemFields.Logo.rawValue].stringValue
self.status = json[LocItemFields.Status.rawValue].stringValue
self.company = json[LocItemFields.Company.rawValue].stringValue
self.id = json[LocItemFields.Id.rawValue].stringValue
// MARK: Endpoints
class func endpointForLocItems(long: Double!, lat: Double!) -> String
return Constants.getLocLoadListUrl() + "/" + String(long) + "/" + String(lat) + "/0"
private class func getLocItemsAtPath(path: String!, completionHandler: (LocItemsWrapper?, NSError?) -> Void)
Alamofire.request(.GET, path!)
.responseLocItemsArray response in
if let error = response.result.error
completionHandler(nil, error)
return
completionHandler(response.result.value, nil)
class func getLocItems(long: Double, lat: Double, completionHandler: (LocItemsWrapper?, NSError?) -> Void)
getLocItemsAtPath(LocItems.endpointForLocItems(long,lat: lat), completionHandler: completionHandler)
class func getMoreLocItems(wrapper: LocItemsWrapper?, completionHandler: (LocItemsWrapper?, NSError?) -> Void)
if wrapper == nil || wrapper?.next == nil
completionHandler(nil, nil)
return
getLocItemsAtPath(wrapper!.next!, completionHandler: completionHandler)
extension Alamofire.Request
func responseLocItemsArray(completionHandler: Response<LocItemsWrapper, NSError> -> Void) -> Self
let responseSerializer = ResponseSerializer<LocItemsWrapper, NSError> request, response, data, error in
guard error == nil else
return .Failure(error!)
guard let responseData = data else
let failureReason = "Array could not be serialized because input data was nil."
let error = Error.errorWithCode(.DataSerializationFailed, failureReason: failureReason)
return .Failure(error)
let JSONResponseSerializer = Request.JSONResponseSerializer(options: .AllowFragments)
let result = JSONResponseSerializer.serializeResponse(request, response, responseData, error)
switch result
case .Success(let value):
let json = SwiftyJSON.JSON(value)
let wrapper = LocItemsWrapper()
wrapper.next = json["next"].stringValue
wrapper.previous = json["previous"].stringValue
wrapper.count = json["count"].intValue
var allLocItems:Array = Array<LocItems>()
//print(json)
let results = json["rows"]
//print(results)
for jsonLocItems in results
//print(jsonLocItems.1)
let locItems = LocItems(json: jsonLocItems.1, id: Int(jsonLocItems.0))
allLocItems.append(locItems)
wrapper.locItems = allLocItems
return .Success(wrapper)
case .Failure(let error):
return .Failure(error)
return response(responseSerializer: responseSerializer,
completionHandler: completionHandler)
任何帮助将不胜感激,谢谢。
编辑:我通过更改 LocItemsWrapper 摆脱了错误?到 LocItemsWrapper!但现在我有一个错误说 .responseLocItemsArray 的使用不明确...
【问题讨论】:
【参考方案1】:Xcode 试图告诉您它无法确定要使用哪个 .responseLocItemsArray,请检查您的所有其他类以查看您是否在其他任何地方使用过它。
【讨论】:
解决了,非常感谢以上是关于Alamofire 类型的表达式在没有更多上下文的情况下是模棱两可的的主要内容,如果未能解决你的问题,请参考以下文章