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

q3aa0525  于 2022-11-06  发布在  Swift
关注(0)|答案(2)|浏览(186)

我在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的对象:

IFIN相同日期AND相同名称AND具有类型2

一定是我的琴弦那样:

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中的类型3,如果之前有类型2与相同的日期和名称基本相同。
请说明:
2018-04-05*type2*nameb2018-04-05*type3*nameb具有相同日期和相同名称,但2018-04-05*type3*nameb之前具有类型2(2018年4月5日 * 类型2 * 名称b)?因此2018年4月5日 * 类型3 * 名称b行必须删除
我该怎么做呢?

jq6vz3qz

jq6vz3qz1#

下面的代码可以满足您的需求:

//: 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
carvr3hs

carvr3hs2#

我将从一个简单的(尽管是黑客式的)方法开始:

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进行比较,以确定是否应该删除它)。
完成后,再次将swapLastTwoCompsMap到最后一个数组上,将字符串恢复为以前的格式。

相关问题