2017-04-26 20 views
0

私はそれぞれの国にポートのマッピングを保持する構造体オブジェクトを持っています。文字列と文字列の配列を含むstructオブジェクトのフィルタリング

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

私はすべての国のためにオブジェクトを保持する配列var countryAndPortsArray = [countryAndPorts]()を持っています。

私も私の問題は、filteredArrayの結果である

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 
} 

この機能で、私はcountryAndPortsArrayをフィルタリングしています、ユーザーの検索クエリ

に応じてフィルタ処理countryAndPortsArrayオブジェクトを保持する配列var filteredArray = [countryAndPorts]()を持っているが内のすべてのポートが含まれていますユーザーの検索キーに関係なく、同じ国が特定のポートに一致します。

たとえば、ユーザーが「パリ」を検索すると、filteredArrayにはフランスのすべてのポートが含まれます。ユーザーが「バルセロナ」を検索すると、filteredArrayにはスペインのすべてのポートが含まれます。

「barcelona」または「paris」と一致するポートだけが含まれ、同じ国の他のすべてのポートではありません。

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"]

を返すようにしたいと思う私のテーブルビューの代表者である、それは私の質問

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お手伝いをします。 –

関連する問題