Swift:在数组中组合2个或更多自定义对象 [英] Swift: Combining 2 or more custom objects in array
问题描述
下面是我的自定义对象类。
class用户组:NSObject {
let groupName:字符串
让用户:[CheckIn]?
init(json:JSON){
self.groupName = json [Constants.Models.UserGroups.groupName] .stringValue
self.users = UserGroups.getUserGroupsList(jsonArray:json [Constants.Models.UserGroups.users] .arrayValue)
}
类func getUserGroupsList(jsonArray:[JSON])-> [CheckIn] {
return jsonArray.flatMap({((jsonItem:JSON)-> Checkin in
return CheckIn(json:jsonItem)
})
}
}
我有一系列上述自定义对象。如何通过合并具有相同 groupName 的每个对象的用户,将2个或更多自定义对象组合为一个对象。
下面是我的CheckIn模型:
class CheckIn:NSObject {
let id:字符串
让firstName:字符串
让lastName:字符串
允许纬度:字符串
允许经度:字符串
提示:字符串
init(json:JSON){
self.id = json [Constants.Models.CheckIn.id] .stringValue
self.firstName = json [Constants.Models.CheckIn.firstName]。 stringValue
self.lastName = json [Constants.Models.CheckIn.lastName] .stringValue
self.hint = json [Constants.Models.CheckIn.hint] .stringValue
self.latitude = json [ location] [Constants.Models.CheckIn.latitude] .stringValue
self.longitude = json [ location] [Constants.Models.CheckIn.longitude] .stringValue
}
}
id 字段在CheckIn中不是唯一的。
下面是一个稍微简化的示例,显示了如何组合具有相同组名的组。 / p>
这是 UserGroup
类。 users
现在是一个变量( var
),因为我们将向组中添加元素以将它们组合在一起。
类UserGroups:NSObject {
let groupName:字符串
var用户:[String]?
init(groupName:字符串,用户:[String]?){
self.groupName = groupName
self.users =用户
}
}
这里是三个组,其中两个共享相同的组名, Blues
。
let group1 = UserGroups(groupName: Blues,用户:[ Tom, Huck, Jim])
let group2 = UserGroups(groupName: Reds,用户:[ Jo, Ben, Tommy])
let group3 = UserGroups(groupName : Blues,用户:[ Polly, Watson, Douglas])
接下来,我们将所有组放在一个数组中。
let allGroups = [group1,group2,group3]
在这里,我们使用Swift的 reduce 函数,使我们可以将数组缩减为仅具有唯一组名的组。
let compacted = allGroups.reduce([UserGroups](), {partialResult,在
中分组var dupe = partialResult.filter {$ 0.groupName == group.groupName}。如果让dupeGroup = dupe {
dupeGroup.users ?,则第一个
。 append(contentsOf:group.users ?? [])
返回partialResult
}否则{
var newPartialResult = partialResult
newPartialResult.append(group)
返回newPartialResult
}
} )
现在将数组简化为唯一的组,我们使用Swift的 map 函数的帮助。
print(compacted.map {$ 0.users})
//打印[
Optional([ Tom, Huck, Jim, Polly, Watson, Douglas]),
可选([[Jo], Ben, Tommy])
]
Below is my custom object class.
class UserGroups: NSObject {
let groupName: String
let users: [CheckIn]?
init(json:JSON) {
self.groupName = json[Constants.Models.UserGroups.groupName].stringValue
self.users = UserGroups.getUserGroupsList(jsonArray: json[Constants.Models.UserGroups.users].arrayValue)
}
class func getUserGroupsList(jsonArray: [JSON]) -> [CheckIn]{
return jsonArray.flatMap({ (jsonItem: JSON) -> CheckIn in
return CheckIn(json: jsonItem)
})
}
}
I've an array of above custom objects. How can I combine 2 or more custom objects into a single object by merging users of every object having same groupName.
Below is my CheckIn Model:
class CheckIn: NSObject {
let id: String
let firstName: String
let lastName: String
let latitude: String
let longitude: String
let hint: String
init(json: JSON) {
self.id = json[Constants.Models.CheckIn.id].stringValue
self.firstName = json[Constants.Models.CheckIn.firstName].stringValue
self.lastName = json[Constants.Models.CheckIn.lastName].stringValue
self.hint = json[Constants.Models.CheckIn.hint].stringValue
self.latitude = json["location"][Constants.Models.CheckIn.latitude].stringValue
self.longitude = json["location"][Constants.Models.CheckIn.longitude].stringValue
}
}
id field is not unique in CheckIn.
Here's a slightly simplified example that shows how to combine groups that have the same group name.
Here is the UserGroup
class. users
is now a variable (var
) because we will be adding elements to groups to combine them.
class UserGroups: NSObject {
let groupName: String
var users: [String]?
init(groupName: String, users: [String]?) {
self.groupName = groupName
self.users = users
}
}
Here are three groups, two of the share the same group name, Blues
.
let group1 = UserGroups(groupName: "Blues", users: ["Tom", "Huck", "Jim"])
let group2 = UserGroups(groupName: "Reds", users: ["Jo", "Ben", "Tommy"])
let group3 = UserGroups(groupName: "Blues", users: ["Polly", "Watson", "Douglas"])
Next, we'll put all the groups in an array.
let allGroups = [group1, group2, group3]
Here, we use Swift's reduce function to allow us to reduce the array to only groups with unique group names.
let compacted = allGroups.reduce([UserGroups](), { partialResult, group in
var dupe = partialResult.filter {$0.groupName == group.groupName }.first
if let dupeGroup = dupe {
dupeGroup.users?.append(contentsOf: group.users ?? [])
return partialResult
} else {
var newPartialResult = partialResult
newPartialResult.append(group)
return newPartialResult
}
})
The array is now reduced to unique groups, we print out all the groups and their users with the help of Swift's map function.
print(compacted.map { $0.users })
// Prints [
Optional(["Tom", "Huck", "Jim", "Polly", "Watson", "Douglas"]),
Optional(["Jo", "Ben", "Tommy"])
]
这篇关于Swift:在数组中组合2个或更多自定义对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!