Swift 2从包含字符串的数组中删除对象 [英] Swift 2 remove object from array with contains string have
问题描述
我在Swift 2中有字符串数组
I have string array in Swift 2:
var myList : [String] = []
我内部有动态字符串,并使用*
字符myList示例将其爆炸:
And I have dynamic strings inside and I explode them with *
character myList examples:
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 个对象的对象:
I want to delete objects have type3 ones in myList:
如果输入的是相同日期和相同名称,并且具有 type2 一个
IF IN same date AND same name AND have type2 ones
必须是这样的字符串:
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.
I want to delete type3 ones in myList if before have type2 with same date and same name basically.
说明:
2018-04-05*type2*nameb
和2018-04-05*type3*nameb
,具有相同的日期和相同的名称,但2018-04-05*type3*nameb
之前具有 type2 (2018-04-05 * type2 * nameb)?因此2018-04-05 * type3 * nameb行必须删除
2018-04-05*type2*nameb
and 2018-04-05*type3*nameb
, have same date and same name but 2018-04-05*type3*nameb
before have type2(2018-04-05*type2*nameb) ? so 2018-04-05*type3*nameb line must be delete
我该怎么办?
推荐答案
此游乐场代码将执行您想要的操作:
This playground code will do what you want:
//: 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从包含字符串的数组中删除对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!