简体   繁体   中英

Swift : Searchbar localizedCaseInsensitiveContainsString is not working or error

Trying to search using localizedCaseInsensitiveContainsString and getting following error message.

* Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[__NSCFString localizedCaseInsensitiveContainsString:]: unrecognized selector sent to instance 0x1702297a0'

var guests = [Guest]() 
var guestresults = [Guest]()

func searchBar(searchBar: UISearchBar!, textDidChange searchText: String!) {
    guestresults = guests.filter {
        ($0.firstname as NSString).localizedCaseInsensitiveContainsString("\(searchText)")
        //($0.firstname as NSString) == searchText
    } 
}

But when i use compare is working

($0.firstname as NSString) == searchText

Its xcode 6 beta 4 Thanks

localizedCaseInsensitiveContainsString was introduced in iOS 8/OS X 10.10, therefore

(str1 as NSString).localizedCaseInsensitiveContainsString(str2)

crashes with the "unrecognised selector" exception on iOS 7. But you can replace it with the equivalent

(str1 as NSString).rangeOfString(str2, options: .CaseInsensitiveSearch).location != NSNotFound

which works back to iOS 2.0.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM