迅速将长代码转换为短代码

时间:2017-01-19 20:28:11

标签: ios swift

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

}

使用此闭包几乎可以缩短。 member.sort {$0.role < $1.role }然而它按字母顺序排列,这样就可以了,但我需要所有者永远是第一位的。

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
    }
}

我非常感谢任何善良的灵魂可以为我提供的任何帮助:)

1 个答案:

答案 0 :(得分:7)

首先,为Role创建常量,例如,使用enum

enum Role : String {
    case owner = "OWNER"
    case admin = "ADMIN"
    case user = "USER"
}

现在让我们声明角色的排序:

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
    }
}

假设Member看起来像这样(请注意,我使用的是Role枚举,而不是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"),
]

现在您的排序可以简化为:

let sortedMembers = members.sorted { $0.role < $1.role }

要将两个数组排序在一起,我们可以,例如,创建成员对象并对这些对进行排序:

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 }

要获取已过滤的对象,请使用角色再次简化:

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()