【问题标题】:Swift - Replace Array's repeated Strings in multiple ArraysSwift - 在多个数组中替换数组的重复字符串
【发布时间】:2015-04-08 21:44:56
【问题描述】:
class MyClass:UIViewController{
   var myArr = ["John", "Eugene", "George", "John", "Lucy", "George"]
   var myArr2 = ["28 years old", "20 years old", "30 years old", "28 years old", "18 years old", "30 years old"]
   var myArr3 = ["3rd Avenue", "Long Beach rd.", "Hollywood Blvd.", "3rd Avenue", "5th street", "Hollywood Blvd."]

   viewDidLoad(){
      super.viewDidLoad()
      removeRepeatedStringsFromArrays(myArr, [myArr2, myArr3])
   }

   func removeRepeatedStringsFromArrays(keyArray: [String], valuesArrays: [[String]]) -> [[String]]{
   //do the func and I want myArr to be changed directly in func like:
   keyArray = myArr.withoutRepeats
   //and other arrays to be returned with removed repeated indexes from keyArray indexes
   }
}

代码执行后的预期结果:

myArr = ["John", "Eugene", "George", "Lucy"]
myArr2 = ["28 years old", "20 years old", "30 years old", "18 years old"]
myArr3 = ["3rd Avenue", "Long Beach rd.", "Hollywood Blvd.", "5th street"]

函数是否可以在函数内部更改 myArr2 和 myArr3 或者我必须在 [[String]] 中返回它们并执行此操作:

myArr2 = removeRepeatedStringsFromArrays(myArr, [myArr2, myArr3]).0
myArr3 = removeRepeatedStringsFromArrays(myArr, [myArr2, myArr3]).1

?

【问题讨论】:

  • 只是为了检查一下——你有一个数组,你想删除重复项,还要从其他数组中删除相同位置的相应条目?

标签: arrays string swift replace


【解决方案1】:

不,您将无法编写一个函数,向其中传递一个数组数组并从源数组中删除条目。您将不得不直接在方法中删除它们。您可以通过首先创建一个仅查找第一个唯一条目的索引的函数来做到这一点:

func indicesOfUniques<T: Hashable>(source: [T]) -> [Int] {
    var seen: Set<T> = []
    return filter(indices(source)) {
        if seen.contains(source[$0]) {
            return false
        }
        else {
            seen.insert(source[$0])
            return true
        }
    }
}

然后,使用按特定索引过滤的助手:

extension Array {
    func filterByIndex<S: SequenceType where S.Generator.Element == Int>(indices: S) -> [T] {
        return Array(PermutationGenerator(elements: self, indices: indices))
    }
}

您删除所有相应的索引:

class MyClass {
    var myArr = ["John", "Eugene", "George", "John", "Lucy", "George"]
    var myArr2 = ["28 years old", "20 years old", "30 years old", "28 years old", "18 years old", "30 years old"]
    var myArr3 = ["3rd Avenue", "Long Beach rd.", "Hollywood Blvd.", "3rd Avenue", "5th street", "Hollywood Blvd."]

    func viewDidLoad() {
        removeRepeatedStringsFromArrays()
    }

    func removeRepeatedStringsFromArrays() {

        let uniques = indicesOfUniques(myArr)
        myArr = myArr.filterByIndex(uniques)
        myArr2 = myArr2.filterByIndex(uniques)
        myArr3 = myArr3.filterByIndex(uniques)

    }
}

但是,您可能会发现完全放弃数组并将这些数据表示为数据结构要容易得多:

struct Person {
  let name: String
  let age: Int
  let address: String
}

class MyClass:UIViewController{
    var people = [
        Person(name: "John", age: 28, address: "3rd Avenue"),
        Person(name: "Eugene", age: 20, address: "Long Beach rd."),
        // etc...
    ]

    viewDidLoad() {
        super.viewDidLoad()
        var seen: Set<String> = []
        people = people.filter {
            if seen.contains($0.people.name) {
                return false
            }
            else {
                seen.insert($0.people.name)
                return true
            }
        }
    }
}

【讨论】:

  • 我在数组扩展 Use of undeclared type 'Index'Cannot invoke initializer for type 'PremutationGenerator&lt;C, Indices&gt;' with an argument list of type '(elements: Array&lt;T&gt;, indices: S)' 上收到错误我知道我可以使用数据结构,但我的所有信息都在数组中,我需要你写的这个方法,但是有数组扩展出错..
  • 糟糕,抱歉,我没有机会直接运行它,事实上,我的代码中有几个地方的逻辑不正确。更新了,看看是否适合你。
  • 现在可以使用了! :) 非常感谢你能告诉我从哪里可以学习 Pure Swift 吗?
【解决方案2】:

swift 中的数组使用值语义,并按值传递给方法 和函数(它们不是通过引用传递的)。因此,如果您在方法内部修改数组,这不会在该方法的范围之外产生任何影响。要通过引用传递数组并且能够修改它,您应该传递它使用参数运算符“inout”使其成为输入输出参数。输入输出实际上意味着通过引用而不是按值传递值。因此,如果您决定按值传递数组,则需要像当前一样返回它们,但如果您通过引用传递数组,则不需要返回任何内容。

还要检查数组是否包含重复项,您可以使用这个通用方法:

func distinct<T: Equatable>(source: [T]) -> [T] 
{
  var unique = [T]()
  for item in source {
    if !contains(unique, item) 
    {
       unique.append(item)
    }
  }
  return unique
}

【讨论】:

    猜你喜欢
    • 2011-06-18
    • 1970-01-01
    • 2019-01-14
    • 2018-06-16
    • 1970-01-01
    • 2017-10-09
    • 2020-11-09
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多