ios - Swift - Search always executing before full text -


i have class handles user looking user via username. looking part done through searchbar control. backend firebase.

here full code have:

class addfriendbyusernametableviewcontroller: uitableviewcontroller, uisearchbardelegate, uisearchdisplaydelegate {      /**************************global variables************************/     var friendobject = friendclass()     var friendsarray = [friendclass]()     var friendsusernames = [string]()     var isfirstloading: bool = true     var utiltiies = utilities()     var searchactive : bool = false     var usernames:[string]!     /**************************ui components************************/     @iboutlet weak var searchbar: uisearchbar!       override func viewdidload() {         super.viewdidload()         self.searchbar.delegate = self      }      override func didreceivememorywarning() {         super.didreceivememorywarning()         // dispose of resources can recreated.     }      func search(searchtext: string? = nil){          /****************************get username auth data****************************/         if(searchtext != nil)         {             self.getallusersforsearchfilter({(result) -> void in                 if(result.domain == "")                 {                     let containsresult = self.usernames.contains(searchtext!)                     if(containsresult == true)                     {                         /*query information found username*/                         let reference = firebase(url:"https://something.firebaseio.com/users/")                         /****************************get username auth data****************************/                         reference.queryequaltovalue(searchtext!).observeeventtype(.value, withblock: { (snapshot: fdatasnapshot!) -> void in                              userinstance in snapshot.children.allobjects as! [fdatasnapshot]{                              }                         })                        }else{                         print("no matching username search text")                     }                 }             })         }       }       func getallusersforsearchfilter(completion: (result: nserror) -> void)     {         let errorfound:nserror = nserror(domain: "", code: 0, userinfo: nil)          let reference = firebase(url:"https://something.firebaseio.com/users/")         reference.observeeventtype(.value, withblock: { (snapshot: fdatasnapshot!) -> void in             if(snapshot != nil )             {                 userinstance in snapshot.children.allobjects as! [fdatasnapshot]{                     //username = (userinstance.value["username"] as? string)!                     //self.usernames.append(userinstance.value["username"] as! string)                 }                 completion(result: errorfound)             }else{                 completion(result: errorfound)             }          })     }       // mark: - table view data source      override func numberofsectionsintableview(tableview: uitableview) -> int {         // #warning incomplete implementation, return number of sections         return 1     }      override func tableview(tableview: uitableview, numberofrowsinsection section: int) -> int {         // #warning incomplete implementation, return number of rows          if(usernames != nil)         {             return usernames.count         }         return 0     }      override func tableview(tableview: uitableview, cellforrowatindexpath indexpath: nsindexpath) -> uitableviewcell {         let cell = tableview.dequeuereusablecellwithidentifier("cell", forindexpath: indexpath) uitableviewcell           //self.tempobjectholder = self.data[indexpath.row]         //cell.textlabel!.text = self.tempobjectholder["appusername"] as? string           return cell     }      override func tableview(tableview: uitableview, didselectrowatindexpath indexpath: nsindexpath) {          let indexpatht = tableview.indexpathforselectedrow;         let currentcell = tableview.cellforrowatindexpath(indexpatht!)          /*get username selected search results. */         self.friendobject.username = currentcell?.textlabel!.text          /*self.friendobject.name = self.tempobjectholder["name"] as? string         self.friendobject.mobile = self.tempobjectholder["mobile"] as? string         self.friendobject.telephone = self.tempobjectholder["telephone"] as? string         self.friendobject.username = self.tempobjectholder["appusername"] as? string         self.friendobject.email = self.tempobjectholder["email"] as? string         self.friendobject.workaddressstring = self.tempobjectholder["workaddress"] as! string         self.friendobject.homeaddressstring = self.tempobjectholder["homeaddress"] as! string*/          self.performseguewithidentifier("viewuserresultssegue", sender: self)      }      func searchbartextdidbeginediting(searchbar: uisearchbar) {         searchactive = true;     }      func searchbartextdidendediting(searchbar: uisearchbar) {         searchactive = false;     }      func searchbarcancelbuttonclicked(searchbar: uisearchbar) {         searchactive = false;     }      func searchbarsearchbuttonclicked(searchbar: uisearchbar) {         search()         searchactive = false;     }      func searchbar(searchbar: uisearchbar, textdidchange searchtext: string) {         search(searchtext)     }       override func prepareforsegue(segue: uistoryboardsegue, sender: anyobject!) {         if (segue.identifier == "viewuserresultssegue") {             // pass data next view             //let destinationvc = segue.destinationviewcontroller as! viewresultuserprofileviewcontroller             //destinationvc.friendobject = self.friendobject;          }     } } 

when start typing in the search bar, executes , triggers erro:

fatal error: unexpectedly found nil while unwrapping optional value

and happening because took firt character entered , searched it. want search full work (username) not first letters.

thanks,

to action when keyboard enter tapped, can create ibaction method in view controller, such as:

@ibaction func enterdetected(sender: uitextfield) {     print("saw 'enter'") } 

then connect uitextfield's "primary action triggered" connection method in interface builder.


Comments

Popular posts from this blog

javascript - jQuery: Add class depending on URL in the best way -

caching - How to check if a url path exists in the service worker cache -

Redirect to a HTTPS version using .htaccess -