【问题标题】:Swift - Trim and map the first letter from a ArraySwift - 修剪和映射数组中的第一个字母
【发布时间】:2020-12-18 01:00:55
【问题描述】:

我有一个包含 >2500 个客户姓名的列表,并希望创建一个按字母顺序排列的 lastNames 索引(lastname 的第一个字母)。 如果 LastName 前面有空格(我知道 Legacy dataissue 应该稍后会修复),它们应该被删除。如果没有可用的 LastName,那么它应该作为 indexArrayro 中的最后一项出现。

结果应该是 indexArray == ["A","G","I","M","S","Z",""] 大写。

这是我目前得到的。

struct MyObject {
    let firstName: String
    let lastName : String
}

let objectArray : [MyObject?] = [
    MyObject(firstName: "piet", lastName: "mozilla"),
    MyObject(firstName: "jol", lastName: " godzilla"),
    MyObject(firstName: "", lastName: "Anubis"),
    MyObject(firstName: "paul", lastName: ""),
    MyObject(firstName: "", lastName: "Strawberry"),
    nil,
    MyObject(firstName: "jol", lastName: "zwordfish"),
    MyObject(firstName: "jol", lastName: ""),
    MyObject(firstName: "", lastName: "Zwordfish"),
    nil,
    MyObject(firstName: "pot", lastName: "IronFly")
]


extension Array {  
    mutating func rearrange(from: Int, to: Int) {
        insert(remove(at: from), at: to)
    }
}

extension String
{   
    func trim() -> String
    {
        return self.trimmingCharacters(in: .whitespaces)
    }
}

let arrayWithNoNils = objectArray.flatMap { $0 } // to remove the 'nil's

let indexedArray = arrayWithNoNils.map({ $0.lastName.prefix(1) }) // get the first character

var indexArr : Array = Set(indexedArray).sorted() as Array // make it unique and sort

if indexArr.count > 0 && indexArr[0] == "" { // move the empty one as last
    let cnt = indexArr.count - 1
    indexArr.rearrange(from: 0, to: cnt)
}

对 Trim 有帮助吗? 我以为我可以在里面使用它

arrayWithNoNils.map({ $0.lastName.prefix(1) })

类似

arrayWithNoNils.map({ $0.lastName.trim.prefix(1) })

对此有何建议?

一如既往地谢谢你。

【问题讨论】:

  • 您能解释一下当前代码的预期结果和实际结果吗?
  • 请为您包含的示例数据添加预期输出。目前还不清楚你想要实现什么。
  • 说得好,因为现在我记得我希望结果是大写的。

标签: swift dictionary indexing flatmap


【解决方案1】:

这将为您提供索引数组,方法是使用 compactMap 删除 nil 对象并修剪并获取姓氏的第一个字符。然后将其强制转换为 Set 以删除重复项,最后在闭包确保 "" 排在最后的位置进行排序

let indexArray = Set(objectArray
    .compactMap { $0?.lastName.trimmingCharacters(in: .whitespaces).prefix(1).uppercased()})
    .sorted(by: {
        if $0.isEmpty {
            return false
        }
        if $1.isEmpty {
            return true
        }
        return $0 < $1
    })

【讨论】:

  • 在 SwiftUI 的右侧列出一个索引(按钮),用户可以单击该索引以滚动到该字母对应的姓氏的字母。
  • @LordAnubis 好的,我没有看到你问题底部的代码
  • 我想我会使用你的并从中创建一个扩展。
【解决方案2】:

您可以尝试以下方法:

let firstLetters = objectArray.map { item -> String in
    guard let item = item else { return "" }
    return String(item.lastName.trim().prefix(1)).uppercased()
}

let indexArray = Array(Dictionary(grouping: firstLetters, by: { $0 }).keys)
    .sorted {
        if $0 == "" { return false }
        if $1 == "" { return true }
        return $0 < $1
    }

print(indexArray) // ["A", "G", "I", "M", "S", "Z", ""]

【讨论】:

    【解决方案3】:

    这就是你要找的东西吗?

    let fixedArray = objectArray.map({ (object) -> MyObject? in
        guard let existedObject = object else { return nil }
        return MyObject(firstName: existedObject.firstName, lastName: existedObject.lastName.trimmingCharacters(in: .whitespacesAndNewlines))
    })
    

    【讨论】:

      【解决方案4】:

      我找到了,

      这条线应该变成

      let indexedArray = arrayWithNoNils.map({ (($0.lastName.trim)().uppercased()).prefix(1)
      

      【讨论】:

        猜你喜欢
        • 2019-06-18
        • 1970-01-01
        • 1970-01-01
        • 2021-03-20
        • 2020-10-15
        • 2017-05-22
        • 2023-03-29
        • 2020-05-13
        • 1970-01-01
        相关资源
        最近更新 更多