按 swift 中的两个值分组

groupBy array by two values in swift

我正在使用 Array 到 groupBy 的这个扩展:

func groupBy <U> (groupingFunction group: (Element) -> U) -> [U: Array] {

    var result = [U: Array]()

    for item in self {

        let groupKey = group(item)

        // If element has already been added to dictionary, append to it. If not, create one.
        if result.has(groupKey) {
            result[groupKey]! += [item]
        } else {
            result[groupKey] = [item]
        }
    }

    return result
}

并尝试根据 teamId 和等级(我的对象的自定义属性来自数组)制作字典。

  self.items = myArray.groupBy {
    team in

    if team.rank == nil {
      return team.teamId!
    } else {
      return team.rank!
    }
  }

我的情况是这样的:

[{
"team_id": "4",
"team_name": "T16",
"rank": "3"
},
,{
"team_id": "4",
"team_name": "T16",
"rank": "2"
},
{
"team_id": "4",
"team_name": "T16",
"rank": "1"
}
,{
"team_id": "5",
"team_name": "T16",
"rank": null
}]

期望的输出:

let teams : [String: TeamItem] = [ "4" : TeamItem.rank3, "4-2" : TeamItem.rank2, "4-1" : TeamItem.rank1, "5" : TeamItem]

编辑 2

func requestTeamData(listener:([TeamItem]) -> ()) {
    dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0)) {

      let baseURL: String = self._appSettings.getPathByName(EndPoint.BASE_URL.rawValue) as! String
      let paths: NSDictionary = self._appSettings.getPathByName(EndPoint.PATH.NAME.rawValue) as! NSDictionary

      let teamPaths: NSDictionary = paths.objectForKey(EndPoint.PATH.TEAM.KEY.rawValue) as! NSDictionary
      let path = teamPaths.valueForKey(EndPoint.PATH.TEAM.PATH.rawValue) as! String

      request(.GET, baseURL + path, parameters:[:])
        .responseCollection { (_, _, team: [TeamItem]?, error) in

          if let team = team {

            dispatch_async(dispatch_get_main_queue()) {
              listener(team)
            }
          }
      }
    }
  }

直接调用API

DataProvider.getInstance().requestTeamData({(items:[TeamItem]) -> () in

//Getting the items from the API

})

编辑 1

@objc final class TeamItem: Equatable, ResponseObjectSerializable, ResponseDictionarySerializable {

  let firstName: String?
  let lastName: String?
  let seasonId: String?
  let seasonName: String?
  let teamId: String?
  let teamName: String?
  let rank: String?
  let positionId: String?
  let positionName: String?
  let number: String?
  let birthDate: String?
  let birthPlace: String?
  let citizenship: String?
  let height: String?
  let weight: String?
  let maritalStatus: String?
  let model: String?
  let hobby: String?
  let food: String?
  let book: String?
  let movie: String?
  let wish: String?
  let message: String?
  let biography: String?
  let imageURL: String?
  let teamImageURL: String?

  required init?(response: NSHTTPURLResponse, representation: AnyObject) {

    self.firstName = representation.valueForKeyPath(EndPoint.Keys.Team.FirstName.rawValue) as? String
    self.lastName = representation.valueForKeyPath(EndPoint.Keys.Team.LastName.rawValue) as? String
    self.seasonId = representation.valueForKeyPath(EndPoint.Keys.Team.SeasonId.rawValue) as? String
    self.seasonName = representation.valueForKeyPath(EndPoint.Keys.Team.SeasonName.rawValue) as? String
    self.teamId = representation.valueForKeyPath(EndPoint.Keys.Team.TeamId.rawValue) as? String
    self.teamName = (representation.valueForKeyPath(EndPoint.Keys.Team.TeamName.rawValue) as? String)!
    self.rank = representation.valueForKeyPath(EndPoint.Keys.Team.Rank.rawValue) as? String
    self.positionId = representation.valueForKeyPath(EndPoint.Keys.Team.PositionId.rawValue) as? String
    self.positionName = representation.valueForKeyPath(EndPoint.Keys.Team.PositionName.rawValue) as? String
    self.number = representation.valueForKeyPath(EndPoint.Keys.Team.Number.rawValue) as? String
    self.birthDate = representation.valueForKeyPath(EndPoint.Keys.Team.BirthDate.rawValue) as? String
    self.birthPlace = (representation.valueForKeyPath(EndPoint.Keys.Team.BirthPlace.rawValue) as? String)!
    self.citizenship = representation.valueForKeyPath(EndPoint.Keys.Team.Citizenship.rawValue) as? String
    self.height = representation.valueForKeyPath(EndPoint.Keys.Team.Height.rawValue) as? String
    self.weight = representation.valueForKeyPath(EndPoint.Keys.Team.Weight.rawValue) as? String
    self.maritalStatus = representation.valueForKeyPath(EndPoint.Keys.Team.MaritalStatus.rawValue) as? String
    self.model = representation.valueForKeyPath(EndPoint.Keys.Team.Model.rawValue) as? String
    self.hobby = representation.valueForKeyPath(EndPoint.Keys.Team.Hobby.rawValue) as? String
    self.food = representation.valueForKeyPath(EndPoint.Keys.Team.Food.rawValue) as? String
    self.book = representation.valueForKeyPath(EndPoint.Keys.Team.Book.rawValue) as? String
    self.movie = representation.valueForKeyPath(EndPoint.Keys.Team.Movie.rawValue) as? String
    self.wish = representation.valueForKeyPath(EndPoint.Keys.Team.Wish.rawValue) as? String
    self.message = representation.valueForKeyPath(EndPoint.Keys.Team.Message.rawValue) as? String
    self.biography = representation.valueForKeyPath(EndPoint.Keys.Team.Biography.rawValue) as? String
    self.imageURL = representation.valueForKeyPath(EndPoint.Keys.Team.ImageURL.rawValue) as? String
    self.teamImageURL = representation.valueForKeyPath(EndPoint.Keys.Team.TeamImageURL.rawValue) as? String

  }


        @objc static func collection(#response: NSHTTPURLResponse, representation: AnyObject) -> [TeamItem] {
        var teamItemArray: [TeamItem] = []

        if let representation = representation as? [[String: AnyObject]] {
          for teamItemRepresentation in representation {
            if let teamItem = TeamItem(response: response, representation: teamItemRepresentation) {
              teamItemArray.append(teamItem)
            }
          }
        }

        return teamItemArray
      }

}

func ==(lhs: TeamItem, rhs: TeamItem) -> Bool {
  return lhs.lastName == rhs.lastName
}
}

编辑

TeamItem.rank1实际上表示rank的值为1(其他同理)

这是需要的,希望能帮助到某人:

@objc static func dictionary(#response: NSHTTPURLResponse, representation: AnyObject) -> [String: Array<TeamItem>] {

    var teamItemDictionary: [String: Array<TeamItem>] = [:]

    if let representation = representation as? [[String: AnyObject]] {
      for teamItemRepresentation in representation {
        if let teamItem = TeamItem(response: response, representation: teamItemRepresentation) {

          if teamItem.rank != nil {
            let teamKey = teamItem.teamId! + "-" + teamItem.rank!

            if teamItem.teamId != EndPoint.Keys.Team.Type.rawValue {

              if let team = teamItemDictionary[teamKey] as [TeamItem]? {
                teamItemDictionary[teamKey]?.push(teamItem)
              } else {
                teamItemDictionary[teamKey] = []
                teamItemDictionary[teamKey]?.push(teamItem)
              }
            }
          } else {
            if teamItem.teamId != EndPoint.Keys.Team.Type.rawValue {

              if let team = teamItemDictionary[teamItem.teamId!] as [TeamItem]? {
                teamItemDictionary[teamItem.teamId!]?.push(TeamItem)
              } else {
                teamItemDictionary[teamItem.teamId!] = []
                teamItemDictionary[teamItem.teamId!]?.push(TeamItem)

              }
            }
          }
        }
      }
    }

    return teamItemDictionary
  }