2017-04-26 64 views
0

我有一個結構對象,它持有端口到其各自國家的映射。過濾包含字符串和字符串數組的結構對象

struct countryAndPorts { 
    var country: String? 
    var ports: [String]? 
} 

我有一個適用於每個國家對象數組var countryAndPortsArray = [countryAndPorts]()

我也有一個數組var filteredArray = [countryAndPorts]()持有過濾取決於用戶的搜索查詢

我與這個功能過濾countryAndPortsArraycountryAndPortsArray對象

func filteredContent(searchKey: String) { 
    filteredArray = countryAndPortsArray.filter { 
     !($0.ports?.filter{ 
      $0.range(of: searchKey, options: .caseInsensitive, range: 
      nil, locale: nil) != nil || 
      $0.localizedCaseInsensitiveCompare(searchKey) == .orderedSame 
      }.isEmpty ?? true) || 
     $0.country?.localizedCaseInsensitiveContains(searchKey) ?? true 
} 

我的問題是filteredArray結果包含內的每一個端口無論用戶的搜索關鍵字是否與特定端口匹配,都是同一個國家。

例如,如果用戶搜索「巴黎」,filteredArray將包含法國的每個端口。如果用戶搜索「巴塞羅那」,filteredArray將包含西班牙的每個港口。

期望的結果應該只包含匹配「巴塞羅那」或「巴黎」的端口,而不是同一個國家內的所有其他端口。

UPDATE

使用案例:

var france = countryAndPorts(country: "France", ports: 
["monaco","paris","ajaccio","lyon"]) 

var spain = countryAndPorts(country: "Spain", ports: ["barcelona", 
"madrid", "catalan"]) 

var countryAndPortsArray = [france,spain] 

如果我搜索countryAndPortsArray巴塞羅那,它返回["barcelona", "madrid", "catalan"]。但是我希望它只是返回["barcelona"]

,這裏是我的tableview代表只是櫃面它揭示更多的光線,我的問題

extension SearchDealsViewController: UITableViewDelegate, 
UITableViewDataSource { 

func tableView(_ tableView: UITableView, numberOfRowsInSection section: 
Int) -> Int { 
    if let count = filteredArray[section].ports?.count { 
     return count 
    } 
     return 0 
} 

func tableView(_ tableView: UITableView, cellForRowAt indexPath: 
IndexPath) -> UITableViewCell { 
    let cell = tableView.dequeueReusableCell(withIdentifier: "cell", 
for: 
indexPath) 

    if let port = filteredArray[indexPath.section].ports? 
[indexPath.row] { 
     cell.textLabel?.text = port 
    } 
    cell.textLabel?.font = UIFont.systemFont(ofSize: 10) 
    return cell 
} 

func numberOfSections(in tableView: UITableView) -> Int { 
    return filteredArray.count 
} 

func tableView(_ tableView: UITableView, didSelectRowAt indexPath: 
IndexPath) { 
    portsFromTextField.text = filteredArray[indexPath.section].ports? 
    [indexPath.row] 
} 

func tableView(_ tableView: UITableView, viewForHeaderInSection 
section: Int) -> UIView? { 
    let header = tableView.dequeueReusableCell(withIdentifier: 
"header") 
    let tapSectionHeaderGesture = UITapGestureRecognizer(target: self, 
    action: #selector(getCountryText(sender:))) 
    tapSectionHeaderGesture.numberOfTouchesRequired = 1 
    tapSectionHeaderGesture.numberOfTapsRequired = 1 
    header?.addGestureRecognizer(tapSectionHeaderGesture) 

if header != nil { 
    header?.textLabel?.textColor = THEME_COLOUR 
    header?.textLabel?.font = UIFont.boldSystemFont(ofSize: 12) 
    header?.textLabel?.text = filteredArray[section].country 
} 

return header 
} 

func getCountryText(sender: UITapGestureRecognizer) { 
    if let country = sender.view as? UITableViewCell { 
     portsFromTextField.text = country.textLabel?.text 
    } 
} 

func tableView(_ tableView: UITableView, heightForHeaderInSection 
section: Int) -> CGFloat { 
    return 30 
} 
+0

你能不能提供期望的結果小數據的例子。 –

+0

@OlegGordiichuk更新 –

回答

0

我寫的解決方案,但它並非十全十美。但它仍然會返回預期的結果。

首先,我們可以編輯結構以獲取首選結果。

struct countryAndPorts { 
    var country: String? 
    var ports: [String]? 

    func getPort(_ withName: String) -> [String]? { 
     return ports?.filter{$0.lowercased() == withName.lowercased()} 
    } 
} 

不是過濾數據的函數。這不是完美的,但它可以減少flatMap調用的數量。

func fetch(port withName: String, from ports: [countryAndPorts]) -> [String]? { 
    return ports.map{$0.getPort(withName)}.flatMap{$0}.filter{($0?.count)! > 0}.flatMap{$0}.first 
} 

結果:

fetch(port: "madrid", from: countryAndPortsArray) // ["madrid"]? 
+0

嗨,感謝您的回答。儘管我已經設法編寫了一個完美的函數,但我仍然會測試你的答案並回復你。 –

+0

@Remedy_man高興地幫助。 –

相關問題