通过按字母顺序排列的键将数组插入swift字典

时间:2016-02-21 05:53:38

标签: swift dictionary

我有swift struct,如下所示

Dog(
   'breed': 'Affenpoo',
   'age': '1'
)

我希望将此Dog struct插入swift dictionary myDict,其中包含密钥为单个字母,其中包含breed和数组的第一个后一个字母Dog s作为值。

var myDict = [String:[Dog]]

我有来自服务器获取的狗列表,以及for循环如何使用键作为breed的第一个后缀插入myDict。

来自服务器的数据

Dog(
    breed :"Boxer",
    age: 3
),
Dog(
    breed : "Affenpoo",
    age: 1
),
Dog(
    breed :"Affenpug",
    age: 1),
Dog(
    breed :"Affenshire",
    age: 2),
Dog(
    breed :"Bagle Hound",
    age: 2
),
Dog(
    breed :"Affenwich",
    age: 2
),
Dog(
    breed :"Afghan Collie",
    age: 2
),
Dog(
    breed :"Afghan Hound",
    age: 3
)




for dog in dogs {
   //here i have to insert dogs to myDict, and key as first letter of the dog.breed

}

最后我想让myDict保存如下数据。

  ["A":
    [Dog( breed : "Affenpoo", age: 1),
     Dog( breed :"Affenpug", age: 1),
     Dog( breed :"Affenshire", age: 2),
     Dog( breed :"Affenwich", age: 2),
     Dog( breed :"Afghan Collie", age: 2),
     Dog( breed :"Afghan Hound", age: 3)],
 "B":
    [Dog( breed :"Bagle Hound", age: 2),
     Dog( breed :"Boxer", age: 3)]
]

2 个答案:

答案 0 :(得分:2)

狗结构

首先像这样更新Dog结构

struct Dog {
    let breed: String
    let age: Int

    init?(breed: String, age:Int) {
        guard breed.isEmpty == false else { return nil }
        self.breed = breed
        self.age = age
    }

    var firstChar: Character {
        return breed.characters.first!
        // this is safe because empty values for breed are not alloved by the initializer
    }
}

狗的名单

现在给出以下狗列表

let dogs = [
    Dog(breed: "Boxer", age: 3),
    Dog(breed: "Affenpoo", age: 1),
    Dog(breed: "Affenpug", age: 1),
    Dog(breed: "Affenshire", age: 2),
    Dog(breed: "Bagle Hound", age: 2),
    Dog(breed: "Affenwich", age: 2 ),
    Dog(breed: "Afghan Collie", age: 2),
    Dog(breed: "Afghan Hound", age: 3)
].flatMap { $0 }

您可以将值分组为dict

let dict = dogs.reduce([Character:[Dog]]()) { (var dict, dog) -> [Character:[Dog]] in
    if var list = dict[dog.firstChar] {
        list.append(dog)
        dict[dog.firstChar] = list
    } else {
        dict[dog.firstChar] = [dog]
    }
    return dict
}

有序打印

最后,您可以获得按键的排序列表,以获得有序打印

dict.keys.sort().forEach { print("\($0): \(dict[$0]!)", terminator: "\n") }

输出:

A: [Dog(breed: "Affenpoo", age: 1), Dog(breed: "Affenpug", age: 1), Dog(breed: "Affenshire", age: 2), Dog(breed: "Affenwich", age: 2), Dog(breed: "Afghan Collie", age: 2), Dog(breed: "Afghan Hound", age: 3)]
B: [Dog(breed: "Boxer", age: 3), Dog(breed: "Bagle Hound", age: 2)]

答案 1 :(得分:1)

获取前缀,如果密钥存在,则附加项目,如果不创建密钥

for dog in dogs {
  let prefix = dog.breed.substringToIndex(dog.breed.startIndex.successor())
  if myDict.keys.contains(prefix) {
    myDict[prefix]!.append(dog)
  } else {
    myDict[prefix] = [dog]
  }
}

如果您需要按字母顺序使用密钥,请使用额外的数组

var keys = [String]()

for dog in dogs {
  let prefix = String(dog.breed[dog.breed.startIndex])
  if keys.contains(prefix) {
    myDict[prefix]!.append(dog)
  } else {
    myDict[prefix] = [dog]
    keys.append(prefix)
  }
}
keys.sortInPlace(<)