当两个数组都由其自己的结构定义时,用第二个数组过滤数组
Filter an array by a second array when both arrays are defined by its own struct
我试图通过 theCaseDataArray 过滤 theDataArray,其中 theDataArray 中的 dataName 与 theCaseDataArray 中的 dataName 相匹配。从代码中可以看出,每个数组都由其自己的结构定义。我已经在代码中标记了需要帮助的行。我试过使用 $0 和我能想到的一切,但没有任何乐趣。我知道论坛上有很多类似的问题,但 none 看到包含针对这种特殊情况的答案。
import UIKit
import SQLite
struct Data_On_Case
{
var dataID: Int64
var dataName: String
}
struct MyData
{
let dataID: Int64
var dataName: String
var dataType: String?
var dataDescription: String?
var pricePaid: Double?
var shipping: Double?
var datePurchased: String?
var dataDuration: String?
var manufacturer: String?
var venue: String?
var performanceLevel: String?
var dataNotes: String?
var dataStyle: String?
}
class Test_VC: UIViewController
{
private let crossRefTable = Table("CrossRefCase_Data")
private let crossRefRCaseID = Expression<Int64>("CaseID_CaseXRef")
private let crossRefDataID = Expression<Int64>("DataID_CaseXRef")
private var theDataArray = [MyData]()
private let theDataTable = Table("My_Data")
private let dataID = Expression<Int64>("DataID")
private let data_Name = Expression<String>("Data_Name")
private var searchBarShowing: Bool = false
private var isSearching: Bool = false
private var searchingArray = [MyData]()
private var theCurrentArray = [MyData]()
private var theCaseDataArray = [Data_On_Case]()
@IBOutlet weak var theSearchBar: UISearchBar!
override func viewDidLoad()
{
super.viewDidLoad()
}
}
extension Test_VC: UISearchBarDelegate
{
func searchBar(_ searchBar: UISearchBar, selectedScopeButtonIndexDidChange selectedScope: Int)
{
switch selectedScope
{
case 0:
searchingArray = theDataArray
isSearching.toggle()
case 1:
// Cannot convert value of type '((Data_On_Case) throws -> Bool) throws -> Bool' to expected argument type '(MyData) throws -> Bool'
// The line below is where I'm stuck. The error is shown above.
searchingArray = theDataArray.filter(theCaseDataArray.contains(where: ))
isSearching = true
self.theSearchBar.resignFirstResponder()
theSearchBar.showsScopeBar = true
default:
return
}
}
}
使用filter
如下,
searchingArray = theDataArray.filter { data in
return self.theCaseDataArray.contains(where: { [=10=].dataName == data.dataName })
}
我试图通过 theCaseDataArray 过滤 theDataArray,其中 theDataArray 中的 dataName 与 theCaseDataArray 中的 dataName 相匹配。从代码中可以看出,每个数组都由其自己的结构定义。我已经在代码中标记了需要帮助的行。我试过使用 $0 和我能想到的一切,但没有任何乐趣。我知道论坛上有很多类似的问题,但 none 看到包含针对这种特殊情况的答案。
import UIKit
import SQLite
struct Data_On_Case
{
var dataID: Int64
var dataName: String
}
struct MyData
{
let dataID: Int64
var dataName: String
var dataType: String?
var dataDescription: String?
var pricePaid: Double?
var shipping: Double?
var datePurchased: String?
var dataDuration: String?
var manufacturer: String?
var venue: String?
var performanceLevel: String?
var dataNotes: String?
var dataStyle: String?
}
class Test_VC: UIViewController
{
private let crossRefTable = Table("CrossRefCase_Data")
private let crossRefRCaseID = Expression<Int64>("CaseID_CaseXRef")
private let crossRefDataID = Expression<Int64>("DataID_CaseXRef")
private var theDataArray = [MyData]()
private let theDataTable = Table("My_Data")
private let dataID = Expression<Int64>("DataID")
private let data_Name = Expression<String>("Data_Name")
private var searchBarShowing: Bool = false
private var isSearching: Bool = false
private var searchingArray = [MyData]()
private var theCurrentArray = [MyData]()
private var theCaseDataArray = [Data_On_Case]()
@IBOutlet weak var theSearchBar: UISearchBar!
override func viewDidLoad()
{
super.viewDidLoad()
}
}
extension Test_VC: UISearchBarDelegate
{
func searchBar(_ searchBar: UISearchBar, selectedScopeButtonIndexDidChange selectedScope: Int)
{
switch selectedScope
{
case 0:
searchingArray = theDataArray
isSearching.toggle()
case 1:
// Cannot convert value of type '((Data_On_Case) throws -> Bool) throws -> Bool' to expected argument type '(MyData) throws -> Bool'
// The line below is where I'm stuck. The error is shown above.
searchingArray = theDataArray.filter(theCaseDataArray.contains(where: ))
isSearching = true
self.theSearchBar.resignFirstResponder()
theSearchBar.showsScopeBar = true
default:
return
}
}
}
使用filter
如下,
searchingArray = theDataArray.filter { data in
return self.theCaseDataArray.contains(where: { [=10=].dataName == data.dataName })
}