ios


Search by using NSPredicate - Full name


It`s been almost 3 days and I still can not fix the bug. Simple situation, I would like to search for name of person (Name and surname).
Example:
First I write John (results:John Newman, John Stable, John Carens)
Then I write John with whitespace after (results:nothing)
After that I continue with John N (results:John Newman). I need to keep
names displayed even if I write whitespace in the search.
It is just about the first part of if/else where I work with array of 2+ words. Thanks
- (void)searchForText:(NSString*)searchText
{
NSString *searchTextt = [searchText stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceCharacterSet]];
NSArray *array = [searchTextt componentsSeparatedByString:#" "];
NSString *firstName = searchTextt;
NSString *lastName = searchTextt;
NSString *firstName2 = searchTextt;
NSString *lastName2 = searchTextt;
NSPredicate *predicate = nil;
if ([array count] > 1) {
firstName = array[0];
lastName = array[1];
firstName2 = array[1];
lastName2 = array[0];
predicate = [NSPredicate predicateWithFormat:#"(guestName CONTAINS[cd] %# AND guestSurname CONTAINS[cd] %#)", firstName, lastName];
NSPredicate *predicate2 = [NSPredicate predicateWithFormat:#"(guestSurname CONTAINS[cd] %# AND guestName CONTAINS[cd] %#)", lastName2, firstName2];
NSPredicate *compoundPredicate = [NSCompoundPredicate orPredicateWithSubpredicates:#[predicate, predicate2]];
self.searchResults = [self.mainarray filteredArrayUsingPredicate:compoundPredicate];
} else {
NSPredicate *predicate = [NSPredicate predicateWithFormat:#"guestSurname contains[cd] %#", searchText];
NSPredicate *predicate2 = [NSPredicate predicateWithFormat:#"guestName contains[cd] %#", searchText];
NSPredicate *predicate3 = [NSPredicate predicateWithFormat:#"guestCompany contains[cd] %#", searchText];
NSPredicate *compoundPredicate = [NSCompoundPredicate orPredicateWithSubpredicates:#[predicate, predicate2, predicate3]];
self.searchResults = [self.mainarray filteredArrayUsingPredicate:compoundPredicate];
}
}
EDIT:
- (void)updateSearchResultsForSearchController:(UISearchController *)searchController
{
NSString *searchString = searchController.searchBar.text;
[self searchForText:searchString];
[_maintable reloadData];
}
I have an array like
( {
guestName = John;
guestSurname = Newman; }, {
guestName = John;
guestSurname = Stable; }, {
guestName = John;
guestSurname = Carens; } )
I write fuction
- (void)searchForText:(NSString*)searchText
{
searchTextt = [searchText stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceCharacterSet]];
NSArray *array = [searchTextt componentsSeparatedByString:#" "];
NSString *firstName = searchTextt;
NSString *lastName = searchTextt;
NSPredicate *predicate = nil;
if ([array count] > 1) {
firstName = array[0];
lastName = array[1];
predicate = [NSPredicate predicateWithFormat:#"(guestName CONTAINS[cd] %# AND guestSurname CONTAINS[cd] %#) OR (guestName CONTAINS[cd] %# AND guestSurname CONTAINS[cd] %#)", firstName, lastName, lastName, firstName];
} else {
predicate = [NSPredicate predicateWithFormat:#"guestName CONTAINS[cd] %# OR guestSurnamen CONTAINS[cd] %#", firstName, lastName];
}
NSArray *arrResult = [arr filteredArrayUsingPredicate:predicate];;
}
- (void)updateSearchResultsForSearchController:(UISearchController
*)searchController
{
NSString *searchString = searchController.searchBar.text;
[self searchForText:searchString];
[_maintable reloadData];
}
Try this. It's work for me I am getting result when I enter "john " in textfield.

Related Links

How to use the side buttons control the volume instead of ringing APP
textFieldDidEndEditing vs didSelectRowAtIndexPath, the later not being called
Converting to Swift 3 (Swift and Firebase Project)
load .html from a local directory that is not compiled in my app
iOS Facebook share video Swift
DateFormatter Changes Date by 500 Years
Localytics ios push device token
getting optional with value even after unwrapping it
Use of unresolved identifier 'GMSPlacesClient' in xcode 7.3.1
Unknown Selector Issue
How to crop my App image like iPhone photo gallary
iOS: How to subscribe a customer to an Automatically Renewable Subscription
Add objects into array from UITableViewCell
Moving view closer to camera
My session.dataTaskWithRequest method is not returning any data
How to fetch all WiFi connection

Categories

HOME
elastic-beanstalk
actions-on-google
sql-server-2012
restsharp
netbeans-8
jfreechart
phpmailer
telerik
eps
pie-chart
asciidoc
reload
avr
asp.net-mvc-5.2
fbx
python-2.5
phpbb3
lazy-loading
abaqus
sfsafariviewcontroller
ghost-blog
similarity
semantics
py4j
montecarlo
parallax
mathdotnet
asp.net-identity-2
silverlight-5.0
popupwindow
toastr
nest-thermostat
lync
swipe
vue-router
chai
detach
aem-6
chamilo-lms
docker-image
msal
uitabbarcontroller
azure-management-api
es6-promise
monogame
pci
hostapd
asyncsocket
android-gps
gcc4.8
canopen
jce
webloadui
facebook-canvas
jcuda
findall
qtcpserver
replicate
postmark
kombu
agile-processes
tfs2008
setwindowshookex
git-config
agents-jade
pager
qlpreviewcontroller
dms
angularjs-filter
uploadify
sliding
jca
lidar
flexigrid
mysqladmin
greenfoot
par
cocoon-gem
byte-order-mark
openxls
mui
datacontractserializer
directcompute
keyboard-navigation
jquery-backstretch
fay
dfsort
rcu
ccombobox
xhtml-transitional
adlds
sizzle
word-2010
gigaspaces
rc-shell
harvest
j#
c++builder-xe2
objcopy
openfeint
jdic
code-statistics
corporate-policy
docking
case-tools

Resources

Mobile Apps Dev
Database Users
javascript
java
csharp
php
android
MS Developer
developer works
python
ios
c
html
jquery
RDBMS discuss
Cloud Virtualization
Database Dev&Adm
javascript
java
csharp
php
python
android
jquery
ruby
ios
html
Mobile App
Mobile App
Mobile App