Swift 2从包含字符串的数组中移除对象

时间:2018-04-25 20:11:25

标签: arrays swift string swift2

我在Swift 2中有字符串数组:

var myList              : [String] = []

我内部有动态字符串,我用*字符myList示例爆炸它们:

   print(myList[0])  output = 2018-04-05*type2*namea
   print(myList[1])  output = 2018-04-05*type2*nameb
   print(myList[2])  output = 2018-04-05*type3*nameb
   print(myList[3])  output = 2018-04-06*type3*named

我想在myList中删除 type3 的对象:

IF IN 同一日期 AND 同名并且 type2

必须是我的字符串:

   print(myList[0])  output = 2018-04-05*type2*namea
   print(myList[1])  output = 2018-04-05*type2*nameb
   print(myList[2])  output = 2018-04-06*type3*named

必须删除以下项目:

   print(myList[2])  output = 2018-04-05*type3*nameb

我想删除myList中的type3,如果之前的type2基本上具有相同的日期和相同名称。

说明:

type2 之前,

2018-04-05*type2*nameb2018-04-05*type3*nameb 具有相同日期和相同名称2018-04-05*type3*nameb之前(2018-04-05) * type2 * nameb)?所以2018-04-05 * type3 * nameb行必须删除

我该怎么做?

2 个答案:

答案 0 :(得分:3)

此游乐场代码可以执行您想要的操作:

//: Playground - noun: a place where people can play

import UIKit

let myList = ["2018-04-05*type2*namea",
              "2018-04-05*type2*nameb",
              "2018-04-05*type3*nameb",
              "2018-04-06*type3*named"]

//Define a class that lets us map from a string to a date, type, and name string
class ListEntry {
    let fullString: String

    //define lazy vars for all the substrings
    lazy var subStrings: [Substring] = fullString.split(separator: "*")
    lazy var dateString = subStrings[0]
    lazy var typeString = subStrings[1]
    lazy var nameString = subStrings[2]

    //Create a failable initializer that takes a full string as input 
    //and tries to break it into exactly 3 substrings
    //using the "*" sparator
    init?(fullString: String) {
        self.fullString = fullString
        if subStrings.count != 3 { return nil }
    }
}

print("---Input:---")
myList.forEach { print($0) }
print("------------")

//Map our array of strings to an array of ListEntry objects
let items = myList.compactMap { ListEntry(fullString: $0) }

//Create an output array
var  output: [String] = []

//Loop through each item in the array of ListEntry objects, getting an index for each
for (index,item) in items.enumerated() {

    //If this is the first item, or it dosn't have  type == "type3", add it to the output
    guard index > 0,
        item.typeString == "type3" else {
            print("Adding item", item.fullString)
            output.append(item.fullString)
            continue
    }
    let previous = items[index-1]

    /*
     Add this item if
        -the previous type isn't "type2"
        -the previous item's date doesn't match this one
        -the previous item's name doesn't match this one
     */
    guard previous.typeString == "type2",
        item.dateString == previous.dateString,
        item.nameString == previous.nameString else {
            print("Adding item", item.fullString)
            output.append(item.fullString)
            continue
    }
    print("Skipping item ", item.fullString)
}
print("\n---Output:---")
output.forEach { print($0) }

上面代码的输出是:

---Input:---
2018-04-05*type2*namea
2018-04-05*type2*nameb
2018-04-05*type3*nameb
2018-04-06*type3*named
------------
Adding item 2018-04-05*type2*namea
Adding item 2018-04-05*type2*nameb
Skipping item  2018-04-05*type3*nameb
Adding item 2018-04-06*type3*named

---Output:---
2018-04-05*type2*namea
2018-04-05*type2*nameb
2018-04-06*type3*named

答案 1 :(得分:1)

我会用一种简单的(虽然是hack-ish)方法开始你:

let myList = ["2018-04-05*type2*namea", "2018-04-05*type2*nameb", "2018-04-05*type3*nameb", "2018-04-06*type3*named"]

定义功能:

func swapLastTwoComps(_ s: String) -> String {
    let parts = s.split(separator: "*")
    return [parts[0], parts[2], parts[1]].joined(separator: "*")
}

现在,如果你这样做

let myListS = myList.map {swapLastTwoComps($0)}.sorted() 

你得到了

["2018-04-05*namea*type2", "2018-04-05*nameb*type2", "2018-04-05*nameb*type3", "2018-04-06*named*type3"]

即。 sort已将字符串保留在其等效项的邻近和右侧,因此现在您可以轻松地遍历数组并删除所需的字符串(因为您只需要将每个String的前缀与其左侧的String进行比较)确定是否应该删除)。

完成后,再次将swapLastTwoComps映射到最终数组,将字符串恢复为之前的格式。