使用核心数据从初始化器的字符串数组中减去Stings?

时间:2019-05-14 13:15:09

标签: arrays swift xcode core-data

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

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

1 个答案:

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