基于对象属性的任何对象的过滤器数组

时间:2018-10-24 12:09:30

标签: ios swift filter

我收到这种格式的回复

[
    "1234": {
        "startDate": "1536278400",
        "endDate": "1536796800",
        "playerRank": 1,
        "performance": 100,
        "isProfit": false,
        "members": 1
    },
    "adityaKumar": {
        "startDate": "1536364800",
        "endDate": "1540080000",
        "playerRank": 1,
        "performance": 100,
        "isProfit": false,
        "members": 6
    },
    "madhu60": {
        "startDate": "1539388800",
        "endDate": "1539475200",
        "playerRank": 1,
        "performance": 100,
        "isProfit": false,
        "members": 2
    }
]

此响应的类型为[string:Any]。我必须使用playerRank属性对数据进行排序。我希望输出为[string:Any]类型。

在Swift中这怎么可能?

3 个答案:

答案 0 :(得分:0)

字典未排序

来自swift documentation

  

字典在没有定义顺序的情况下将相同类型的键和相同类型的值之间的关联存储在集合中。

但是,您可以在字典上使用.sorted方法,这将为您提供字典数组(数组是可排序的)。

let sorted = dictionary.sorted(by: { ($0.value["playerRank"]) < ($1.value["playerRank"]) })

在您的原始示例中,sorted看起来像这样

[
    (
      "key": "madhu60",
      "value": {
         "startDate": "1539388800",
         "endDate": "1539475200",
         "playerRank": 1,
         "performance": 100,
         "isProfit": false,
         "members": 2
      }
   )
   ....
]

答案 1 :(得分:0)

解析了JSON后,您将得到类型为 dictionary [String: [String:Any]],即

let data = [
    "1234": [
        "startDate": "1536278400",
        "endDate": "1536796800",
        "playerRank": 1,
        "performance": 100,
        "isProfit": false,
        "members": 1
    ],
    "adityaKumar": [
        "startDate": "1536364800",
        "endDate": "1540080000",
        "playerRank": 1,
        "performance": 100,
        "isProfit": false,
        "members": 6
    ],
    "madhu60": [
        "startDate": "1539388800",
        "endDate": "1539475200",
        "playerRank": 1,
        "performance": 100,
        "isProfit": false,
        "members": 2
    ]
]

您可以简单地使用sorted方法对该字典进行排序,即

let sortedData = data.sorted { 
    if let rank1 = $0.value["playerRank"] as? Int, let rank2 = $1.value["playerRank"] as? Int
    {
        return rank1 < rank2
    }
    return false
}

你很好。

答案 2 :(得分:0)

我在这里为您提供完整的答案。

首先将响应文本转换为合格的JSON数据。

第二个构建自定义的结构来解码JSON数据。

第三种结构

最后一次打印回原始响应文本(您可以使用字符串来获得打印结果。)

第一:

            let json = """
    [
        "1234": {
            "startDate": "1536278400",
            "endDate": "1536796800",
            "playerRank": 4,
            "performance": 100,
            "isProfit": false,
            "members": 1
        },
        "adityaKumar": {
            "startDate": "1536364800",
            "endDate": "1540080000",
            "playerRank": 2,
            "performance": 100,
            "isProfit": false,
            "members": 6
        },
        "madhu60": {
            "startDate": "1539388800",
            "endDate": "1539475200",
            "playerRank": 1,
            "performance": 100,
            "isProfit": false,
            "members": 2
        }
    ]
    """.replacingOccurrences(of: "[", with: "{").replacingOccurrences(of: "]", with: "}").data(using: .utf8)

秒:

    struct Response  {
     struct ResponseData  {
        let name : String
         let startDate : String
         let endDate : String
         let performance : Int
         let playerRank : Int
         let isProfit : Bool
         let members : Int
        }
        var responseData: [ResponseData]
        init (responseData: [ResponseData] = []){
            self.responseData = responseData
        }
    }

     extension Response: Decodable {

        struct DataKey: CodingKey {
                    var stringValue: String
                    init?(stringValue: String) {
                        self.stringValue = stringValue
                    }
                    var intValue: Int? { return nil }
                    init?(intValue: Int) { return nil}

                    static let startDate = DataKey(stringValue: "startDate")!
                    static let endDate = DataKey(stringValue: "endDate")!
                    static let performance = DataKey(stringValue: "performance")!
                    static let playerRank = DataKey(stringValue: "playerRank")!
                    static let isProfit = DataKey(stringValue: "isProfit")!
                    static let members = DataKey(stringValue: "members")!
                }

        public init(from decoder: Decoder) throws {
            var responseData = [ResponseData]()
            let container = try decoder.container(keyedBy: DataKey.self)
            for key in container.allKeys {
                let dataContainer = try container.nestedContainer(keyedBy: DataKey.self, forKey: key)

                let startDate = try dataContainer.decode(String.self, forKey: .startDate)
                 let endDate = try dataContainer.decode(String.self, forKey: .endDate)
                 let performance = try dataContainer.decode(Int.self, forKey: .performance)
                 let playerRank = try dataContainer.decode(Int.self, forKey: .playerRank)
                 let isProfit = try dataContainer.decode(Bool.self, forKey: .isProfit)
                 let members = try dataContainer.decode(Int.self, forKey: .members)
                let each = ResponseData.init(name: key.stringValue, startDate: startDate, endDate: endDate, performance: performance, playerRank: playerRank, isProfit: isProfit, members: members)
                responseData.append(each)
            }
            self.init(responseData: responseData)
        }
    }

    let decoder = JSONDecoder()
    var decodedStore = try decoder.decode(Response.self, from: json!)

第三名:

      decodedStore.responseData.sort{$0.playerRank < $1.playerRank }

最后一个:

     print ("[")
    for each in decodedStore.responseData {
        print ("""
               \(each.name): {
                        "startDate": \(each.startDate),
                        "endDate": \(each.endDate),
                        "playerRank": \(each.playerRank),
                        "performance": \(each.performance),
                        "isProfit": \(each.isProfit),
                        "members": \(each.members)
                        }
            """)
    }
    print ("]\n")