Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- {
- "id": "tokenID-tqkif48",
- "name": "@jade",
- "game": "WoW",
- "age": "18"
- },
- {
- "id": "tokenID-fvkif21",
- "name": "@kalel",
- "game": "WoW",
- "age": "20"
- }
- public typealias Users = [UserModel]
- public struct UserModel: Codable {
- public let id: String
- public let game: String
- // etc...
- enum CodingKeys: String, CodingKey {
- case id
- case game
- // etc...
- guard let url = Bundle.main.url(forResource: "Users", withExtension: "json") else {
- fatalError()
- }
- guard let data = try? Data(contentsOf: url) else {
- fatalError()
- }
- let decoder = JSONDecoder()
- do {
- let response = try decoder.decode([UserModel].self, from: data)
- for userModel in response {
- let userDict: [String:String] = [ userModel.name:userModel.game ]
- let reduction = Dictionary(grouping: userDict.keys) { userDict[$0] ?? "" }.reduce(into: [String:[String]](), { (result, element) in
- if element.value.count > 1 {
- result[element.key] = element.value
- }
- })
- // error catch etc
- }
Add Comment
Please, Sign In to add comment