我需要使用存储的数组对库进行突变。这有效:
var library = ["Monday", "Tuesday","wednesday", "Thursday"]
var libraryOff = ["Monday","wednesday"]
library = Array(Set(library).subtracting(libraryOff))
print(library) //=>["Tuesday", "Thursday"]
但是,我需要它使用类似
var library = ["Monday", "Tuesday","wednesday", "Thursday"]
var libraryOff = [newLibrary]
library = Array(Set(library).subtracting(libraryOff))
newLibrary
是coreData,在打印时显示为“星期一,星期三”,
答案 0 :(得分:0)
Array-Set-Array 的舞蹈很丑陋,有一种更有效的方法
var library = ["Monday", "Tuesday","wednesday", "Thursday"]
let libraryOff = ["Monday","wednesday"] // Why is wednesday lowercased?
library.removeAll{ libraryOff.contains($0) }
要使用逗号分隔的字符串,必须将其拆分并删除可能的前导和尾随空白字符
var library = ["Monday", "Tuesday","wednesday", "Thursday"]
let libraryOff = "Monday, wednesday"
let trimmedLibraryOff = libraryOff.split(separator: ",").map{$0.trimmingCharacters(in: .whitespaces)}
library.removeAll{ trimmedLibraryOff.contains($0) }