我需要使用存储的数组对库进行变异。这有效:

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,在打印时显示为“星期一,星期三”,

最佳答案

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) }

09-05 20:00