【问题标题】:Swift 2 remove object from array with contains string haveSwift 2 从包含字符串的数组中删除对象
【发布时间】:2018-10-06 10:05:51
【问题描述】:

我在 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同一日期 AND 同名 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

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

解释:

2018-04-05*type2*nameb2018-04-05*type3*nameb,有 相同的日期和相同的名称,但 2018-04-05*type3*nameb 之前有 type2(2018-04-05*type2*nameb) ?所以 2018-04-05*type3*nameb 行必须delete

我该怎么做?

【问题讨论】:

  • 我无法理解您的帖子。
  • @DuncanC :) 是的,这很复杂
  • @DuncanC 我想删除 myList 中的 type3 如果之前有 type2 的日期和名称基本相同
  • @DuncanC 这样你就可以看到我写了哪些会被删除
  • 不是问题复杂,而是你对它的描述难以理解。

标签: arrays swift string swift2


【解决方案1】:

这个操场代码会做你想做的事:

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

【讨论】:

  • 我需要在 swift 2 中使用它
  • 斯威夫特 2?!为什么?那是 2 个主要语言版本已过时。
  • C 与最新的 swift 很容易,但 swift 2 不工作。我在 swift 2 中的项目。
【解决方案2】:

我将从一个简单的(尽管是 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"]

即排序已将要删除的字符串与它们的等价物相邻并位于其右侧,因此现在您可以轻松地遍历数组并删除所需的字符串(因为您只需将每个字符串的前缀与紧邻其左侧的字符串进行比较即可确定是否应将其删除)。

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-01-09
    • 1970-01-01
    • 2016-05-28
    • 1970-01-01
    • 2022-11-18
    • 2019-06-06
    • 2019-03-19
    • 2020-02-01
    相关资源
    最近更新 更多