2017-12-17 9 views
0

사람들이 게시물을 게시 할 수있는 간단한 응용 프로그램이있어서 사람들이 게시물을 검색 할 수 있지만 검색 할 수 있도록 검색 바를 만들었습니다. title의 게시물 또는 location의 게시물 또는 제목과 위치의 검색을 동시에 수행 할 수 있습니다. 내 검색은 내 firebase 데이터베이스를 통해 검색하지만 나는 title 또는 location 중 하나만 검색하도록 지정할 수 있습니다. 그것은 동시에 boths 키를 검색 그래서 난 내 코드를 필터링 및 검색이신속한 여러 키 검색 4

func filterContent(searchText:String) { 
    self.filteredUsers = self.userArray.filter{user in 

    // let title = user!["title"] as? String 
     let adress = user!["adress"] as? String 

    // return(title?.lowercased().contains(searchText.lowercased()))! 
     return(adress?.lowercased().contains(searchText.lowercased()))! 


    } 

    tableView.reloadData() 

} 

과 같은

분명히 당신은 내가 오직 하나의 반환 할 수 있음을 알 수 그것을 만들 수있는 약간의 문제가 수색은 당신이 내가이 두 가지를 모두 돌려 줄 수있는 어떤 방법으로 알고 있니?

나는이

func filterContent(searchText:String) { 
    self.filteredUsers = self.userArray.filter{user in 

     let title = user!["title"] as? String 
     let adress = user!["adress"] as? String 


    return(adress?.lowercased().contains(searchText.lowercased()))! && (title?.lowercased().contains(searchText.lowercased()))! 


    } 

    tableView.reloadData() 

} 

을 시도했지만 코드는 예를 들어 제목과 주소를 모두가 "사과"다음이 작동 이름을 지정하면 검색 기능이 작동 할 수 있지만 title 경우 "사과 이름 "adress은"orange "라는 이름이고"apple "또는"orange "를 검색 할 때는 &&이 bool로 작동하므로 아무 것도 표시되지 않으며 titlelocation의 키가 같은 경우에만 작동합니다. 그러므로 나는

func filterContent(searchText:String) { 
    self.filteredUsers = self.userArray.filter{ user in 
    let title = user!["title"] as? String 
    let address = user!["adress"] as? String 
    return title.localizedCaseInsensitiveContains(searchText) == true || address.localizedCaseInsensitiveContains(searchText) == true 
    } 
    tableView.reloadData() 
} 

나는 생각 .. 원점에서 여전히이고 내가 가능성이 검색 필터가 좀 더 가까이 아래에 넣어 모두 titleaddress

답변

0
func filterContent(searchText:String) { 
self.filteredUsers = self.userArray.flatMap { (temp) -> User in 
    return temp 
    }.filter { (aUser) -> Bool in 
    return aUser.address?.localizedCaseInsensitiveContains(searchText) == true || aUser.title?.localizedCaseInsensitiveContains(searchText) == true 
    } 
    tableView.reloadData() 
} 

또는를 검색 할 얻을 수있는 방법을 궁금해 큰 일은 평면 사용자가 nil 사용자를 제거하기를 원할 것입니다. 그런 다음 문자열 맨 위에 localizedCaseInsensitiveContains()를 직접 사용하는 것이 좋습니다. 이것이 도움이되기를 바랍니다.

사용자를 운동장 내부 구조로 선언하여 제대로 작동하는지 확인했습니다. 이 작업을 빠르게 수행하는 가장 좋은 방법은 영역 사용자를 작성한 다음 술어를 사용하여 결과를 필터링하는 것입니다. (실적)