1.)I have these two functions that my team lead wants me to shorten using closures. I'm a new developer and can't figure out the right way to shorten these.
1.)我有这两个功能,我的团队负责人希望我缩短使用闭包。我是一名新开发人员,无法找到缩短这些内容的正确方法。
func sortUsers() {
var sorted = [Member]()
var sortedObjects = [PFObject]()
for j in 0...2 {
if member.count > 0 {
for i in 0...member.count - 1 {
if j == 0 {
if member[i].role == "OWNER" {
sorted.append(member[i])
sortedObjects.append(memberObject[i])
}
} else if j == 1 {
if member[i].role == "ADMIN" {
sorted.append(member[i])
sortedObjects.append(memberObject[i])
}
} else {
if member[i].role == "USER" {
sorted.append(member[i])
sortedObjects.append(memberObject[i])
}
}
}
}
}
member = sorted
memberObject = sortedObjects
}
Can almost be shortened by using this closure. member.sort {$0.role < $1.role }
however it shorts alphabetically and that would be okay, but I need Owner to always be first.
使用这个闭包几乎可以缩短。 member.sort {$ 0.role <$ 1.role}然而它按字母顺序排列,这样就可以了,但我需要所有者永远是第一位的。
2.)This second function I haven't figured anything out for. It has the way we had been doing it and then a closure in it that I tried replacing the longer method with but it didn't work.
2.)这第二个功能我没有想到任何东西。它有我们一直在做的方式,然后在其中的一个闭包,我尝试用更换较长的方法,但它没有工作。
func filterUsers() {
sortUsers()
switch roleSelector.selectedSegmentIndex {
case 0:
filtered = member
filteredObjects = memberObject
tableView.reloadData()
case 1:
filtered = []
filteredObjects = []
for i in 0...member.count - 1 {
if member[i].role == "OWNER" {
filtered.append(member[i])
filteredObjects.append(memberObject[i])
}
}
tableView.reloadData()
case 2:
member.filter { $0.role == "ADMIN" }
tableView.reloadData()
case 3:
filtered = []
filteredObjects = []
for i in 0...member.count - 1 {
if member[i].role == "USER" {
filtered.append(member[i])
filteredObjects.append(memberObject[i])
}
}
tableView.reloadData()
default:
break
}
}
I greatly appreciate any help any kind soul may offer me :)
我非常感谢任何善良的灵魂可以给我的任何帮助:)
1 个解决方案
#1
7
First of all, create constants for Role
, for example, using an enum
:
首先,为Role创建常量,例如,使用枚举:
enum Role : String {
case owner = "OWNER"
case admin = "ADMIN"
case user = "USER"
}
Now let's declare ordering for roles:
现在让我们声明角色的排序:
extension Role : Comparable {
var order: Int {
switch self {
case .owner:
return 0
case .admin:
return 1
case .user:
return 2
}
}
static func < (lhs: Role, rhs: Role) -> Bool {
return lhs.order < rhs.order
}
}
Let's suppose Member
looks like this (note I have used my Role
enum instead of your String
).
让我们假设Member看起来像这样(注意我使用了我的Role enum而不是你的String)。
class Member : CustomDebugStringConvertible {
var role: Role
var name: String
init(role: Role, name: String) {
self.role = role
self.name = name
}
var debugDescription: String {
return self.name
}
}
let members: [Member] = [
Member(role: .user, name: "user1"),
Member(role: .owner, name: "owner"),
Member(role: .user, name: "user2"),
Member(role: .admin, name: "admin2"),
Member(role: .admin, name: "admin2"),
]
Now your sorting can be reduced to:
现在您的排序可以简化为:
let sortedMembers = members.sorted { $0.role < $1.role }
To sort both arrays together, we can, for example, create pairs member-object and sort those pairs:
要将两个数组排序在一起,我们可以,例如,创建成员对象并对这些对进行排序:
let pairs = zip(members, objects)
let sortedPairs = pairs.sorted { $0.0.role < $1.0.role }
let sortedMembers = sortedPairs.map { $0.0 }
let sortedObjects = sortedPairs.map { $0.1 }
To get the filtered objects, let's simplify again using roles:
要获取过滤的对象,让我们再次使用角色进行简化:
if roleSelector.selectedSegmentIndex == 0 { // all selected
filtered = member
filteredObjects = memberObject
tableView.reloadData()
return
}
let roles: [Role] = [.owner, .admin, .user]
let role = roles[roleSelector.selectedSegmentIndex - 1]
filtered = members.filter { $0.role == role }
tableView.reloadData()
#1
7
First of all, create constants for Role
, for example, using an enum
:
首先,为Role创建常量,例如,使用枚举:
enum Role : String {
case owner = "OWNER"
case admin = "ADMIN"
case user = "USER"
}
Now let's declare ordering for roles:
现在让我们声明角色的排序:
extension Role : Comparable {
var order: Int {
switch self {
case .owner:
return 0
case .admin:
return 1
case .user:
return 2
}
}
static func < (lhs: Role, rhs: Role) -> Bool {
return lhs.order < rhs.order
}
}
Let's suppose Member
looks like this (note I have used my Role
enum instead of your String
).
让我们假设Member看起来像这样(注意我使用了我的Role enum而不是你的String)。
class Member : CustomDebugStringConvertible {
var role: Role
var name: String
init(role: Role, name: String) {
self.role = role
self.name = name
}
var debugDescription: String {
return self.name
}
}
let members: [Member] = [
Member(role: .user, name: "user1"),
Member(role: .owner, name: "owner"),
Member(role: .user, name: "user2"),
Member(role: .admin, name: "admin2"),
Member(role: .admin, name: "admin2"),
]
Now your sorting can be reduced to:
现在您的排序可以简化为:
let sortedMembers = members.sorted { $0.role < $1.role }
To sort both arrays together, we can, for example, create pairs member-object and sort those pairs:
要将两个数组排序在一起,我们可以,例如,创建成员对象并对这些对进行排序:
let pairs = zip(members, objects)
let sortedPairs = pairs.sorted { $0.0.role < $1.0.role }
let sortedMembers = sortedPairs.map { $0.0 }
let sortedObjects = sortedPairs.map { $0.1 }
To get the filtered objects, let's simplify again using roles:
要获取过滤的对象,让我们再次使用角色进行简化:
if roleSelector.selectedSegmentIndex == 0 { // all selected
filtered = member
filteredObjects = memberObject
tableView.reloadData()
return
}
let roles: [Role] = [.owner, .admin, .user]
let role = roles[roleSelector.selectedSegmentIndex - 1]
filtered = members.filter { $0.role == role }
tableView.reloadData()