使用重复值Swift 3.0压平字典数组

时间:2017-07-24 11:00:53

标签: ios arrays swift dictionary swift3

我有一个像这样的字典数组:

let arrayofDictionaries:[[String:Any]] = [
    ["nationalCode": "1570158037", "bookId": 327, "orderDate": "2017-07-24 14:11:52", "mobileNo": "09873456789", "isFavorite": false, "price": 45000, "bookName": "آموزش عربي انساني نکته ها"],
    ["nationalCode": "1570158037", "bookId": 366, "orderDate": "2017-07-24 14:11:58", "mobileNo": "09873456789", "isFavorite": false, "price": 19000, "bookName": "آبي عربي پيش1و2انساني"],
    ["nationalCode": "1570158037", "bookId": 327, "orderDate": "2017-07-24 14:11:52", "mobileNo": "09873456789", "isFavorite": false, "price": 45000, "bookName": "آموزش عربي انساني نکته ها"],
    ["nationalCode": "1570158037", "bookId": 366, "orderDate": "2017-07-24 14:11:58", "mobileNo": "09873456789", "isFavorite": false, "price": 19000, "bookName": "آبي عربي پيش1و2انساني"],
]

数组中的Dictionary有固定键,值会改变。 我试过这段代码:

for item in arrayofDictionaries {
    for (kind, value) in item {
        print(kind)
        dic.updateValue(value!, forKey: kind)
    }    
}

但这会消除重复的kies,只返回最后的值。 上面代码的输出将是:

dic = ["nationalCode": "1570158037", "bookId": 366, "orderDate": "2017-07-24 14:11:58", "mobileNo": "09873456789", "isFavorite": false, "price": 19000, "bookName": "آبي عربي پيش1و2انساني"]

我想要的是这样的字典:

let flattenedArray : [String : Any] = [
    "nationalCode": "1570158037", "bookId": 327, "orderDate": "2017-07-24 14:11:52", "mobileNo": "09873456789", "isFavorite": false, "price": 45000, "bookName": "آموزش عربي انساني نکته ها",
    "nationalCode": "1570158037", "bookId": 366, "orderDate": "2017-07-24 14:11:58", "mobileNo": "09873456789", "isFavorite": false, "price": 19000, "bookName": "آبي عربي پيش1و2انساني",
    "nationalCode": "1570158037", "bookId": 327, "orderDate": "2017-07-24 14:11:52", "mobileNo": "09873456789", "isFavorite": false, "price": 45000, "bookName": "آموزش عربي انساني نکته ها",
    "nationalCode": "1570158037", "bookId": 366, "orderDate": "2017-07-24 14:11:58", "mobileNo": "09873456789", "isFavorite": false, "price": 19000, "bookName": "آبي عربي پيش1و2انساني",
]

任何帮助将不胜感激

4 个答案:

答案 0 :(得分:1)

根据您的评论,我们最终会得到您要做的事情 - 您希望将arrayOfDictionaries变为String以便通过请求发布。

可能会有所不同,具体取决于您希望它的外观,但如果您想将arrayOfDictionaries转换为字符串,则可以执行以下操作

var aString : String = ""
for aBook in arrayOfDictionaries {
    if let nationalCode = aBook["nationalCode"] {
        aString.append("nationalCode : " + nationalCode + ", ")
    }
    if let bookId = aBook["bookId"] {
        aString.append("bookId : " + bookId + ", ")
    }
    // and so on, and so forth
}

为什么要手动操作,而不是只用字典上的(key, value)进行迭代?遗憾的是,由于Dictionary被实现为哈希映射,因此无法保证在迭代时您的密钥将以与您声明字典时相同的顺序出现(实际上,几乎可以保证订单将不同)。

但同样,这是你的字符串需要的格式问题。如果订单无关紧要,只需使用(key, value)

进行迭代即可

答案 1 :(得分:1)

do {
        let data = try JSONSerialization.data(withJSONObject: JsoonFactor, options: .prettyPrinted)
        let jsonString = String(data: data, encoding: String.Encoding(rawValue: String.Encoding.utf8.rawValue))
        let jr = (jsonString?.replacingOccurrences(of: "\n", with: "", options: .regularExpression))!
        let jrrr = jr.removingWhitespaces()
        let urlEncodedJson = jrrr.addingPercentEncoding( withAllowedCharacters: .urlQueryAllowed)

答案 2 :(得分:0)

字典意味着与唯一键一起使用,很可能该过程不是消除重复键而是创建本身,字典类甚至具有创建字典的功能,该字典定义规则以决定采用哪个值作为重复条目的正确。

答案 3 :(得分:0)

虽然我也不了解你想要达到的目的,但正如你在评论中提到的create a URL string from a dictionary,只需使用:

func urlString() -> String
    {
        var string = ""

        for key in self.keys
        {
            if let value = self[key]
            {
                string += "&\(key)=\(value)"
            }
        }
        return string
    }