【问题标题】:AnyObject to array in swiftAnyObject快速排列
【发布时间】:2014-11-02 19:35:48
【问题描述】:

给出以下函数:

class func collection(#response: NSHTTPURLResponse,
                       representation: AnyObject) -> [City] {
    return []
}

所以这个函数应该返回一个城市对象数组。我必须以某种方式将 AnyObject 类型的 representation 变量转换为城市数组。

我不知道确切的表示类型是什么,但我可以做类似的事情

println(representation[0])

它会打印对象。任何想法如何将表示转换为 [City] 数组?

更新

在做

println(representation as [City]) 

打印为零。

City.swift:

final class City : ResponseCollectionSerializable {

    let id: String
    let name: String

    class func collection(#response: NSHTTPURLResponse, representation: AnyObject) -> [City] {
        return []
    }
}

这只是从https://github.com/Alamofire/Alamofire#generic-response-object-serialization复制和粘贴它应该将JSON响应序列化为对象:

@objc public protocol ResponseCollectionSerializable {
    class func collection(#response: NSHTTPURLResponse, representation: AnyObject) -> [Self]
}

extension Alamofire.Request {

    public func responseCollection<T: ResponseCollectionSerializable>(completionHandler: (NSURLRequest, NSHTTPURLResponse?, [T]?, NSError?) -> Void) -> Self {
        let serializer: Serializer = { (request, response, data) in
            let JSONSerializer = Request.JSONResponseSerializer(options: .AllowFragments)
            let (JSON: AnyObject?, serializationError) = JSONSerializer(request, response, data)
            if response != nil && JSON != nil {
                return (T.collection(response: response!, representation: JSON!), nil)
            } else {
                return (nil, serializationError)
            }
        }

        return response(serializer: serializer, completionHandler: { (request, response, object, error) in
            completionHandler(request, response, object as? [T], error)
        })
    }
}

【问题讨论】:

  • 可能表示为 [City]。但我认为你应该先找出代表的实际班级
  • @AndreyChernukha 查看我更新的问题。

标签: ios swift


【解决方案1】:

您返回的representation 参数是调用NSJSONSerialization.JSONObjectWithData... 的结果,因此它要么是NSArray,要么是NSDictionary。由于您获得了representation[0] 的值,我们知道它是NSArray。您的代码的确切外观将取决于 JSON(您应该在这样的问题中包含一个示例),但您的代码需要类似于(未测试的代码):

class func collection(#response: NSHTTPURLResponse, representation: AnyObject) -> [City] {
    var cities: [City] = []
    for cityRep in representation {
        // these next two lines should grab the city data using the correct key
        let id = cityRep.valueForKey("cityID") as String
        let name = cityRep.valueForKey("cityName") as String
        // now add the city to our list
        cities.append(City(id: id, name: name))
    } 
    return cities
}

【讨论】:

    【解决方案2】:

    假设(虽然我不想做出假设,但您的问题对细节有点含糊)表示要么是表示响应的 NSData 对象,要么是您从响应创建的数组。

    根据我的经验,这样的响应是一系列可用于创建城市对象的字典。因此,您需要编写一个函数,将该字典转换为City 对象。有签名的东西:

    parser (AnyObject) -> City
    

    现在,您可以遍历数组,将此函数应用于每个字典,将结果收集到一个数组中并返回结果。

    但你可以更优雅,将你的函数映射到数组上并返回结果。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-07-14
      • 1970-01-01
      • 2015-06-25
      • 2015-11-04
      相关资源
      最近更新 更多