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 make different search field for every sections in UITableview?
Im having trouble with using a custom navigation bar in SWIFT
Error playing embedded youtube video iOS 9
How to stop shooting with “NSTimer”
Warning: Attempt to present <GuessMeFinal> on <GuessMeFinal.ViewController> whose view is not in the window hierarchy -Swift
SpriteKit Gravity and Velocity have inconsistent speed on different devices
How bad is it to embed UIWebView in UIScrollView?
How to hide the toolbar when user taps an action in a CNContactViewController?
How Touch drag enter works?
How to fetch all contacts record in iOS 9 using Contacts Framework
Checking value of hundredths in Double
Why do I get a “HTTP load failed” exception after upgrading to XCode 7?
iOS client setup for GCM, need senderID clarification
PFUser.currentUser() never equals nil
Swift 2 .plist dictArray to Remote Json Array Error
Hide status bar in SWRevealViewController

Categories

HOME
jsp
compliance
tags
word-vba
bootstrap-selectpicker
cql3
redhat
streaming
vsix
netbeans-8
add-in
wavelet
marketplace
command-line-arguments
nservicebus
graph-tool
rebus
smartsheet-c#-sdk-v2
phpbb3
lazy-loading
eigen3
gyroscope
libpcap
pwm
rgdal
netcdf
eonasdan-datetimepicker
regex-group
number-formatting
buffer-overflow
debugview
gcp
udid
toastr
jtapi
dashboard-designer
blockui
gradient-descent
extraction
xaf
jupyter-irkernel
bem
dsx
es6-promise
brainfuck
subscription
dxgi
powerpc
autofocus
zuul
simplesamlphp
clientscript
mashery
harvest-scm
gpt
ifc
geneticsharp
informatica-cloud
webdeploy-3.5
sql-server-2016-express
nuitka
aplpy
administrator
pyenchant
axlsx
maven-cargo
size-t
antiforgerytoken
reactive-cocoa-3
horizontalscrollview
nolio
laravel-validation
opensc
spread-toolkit
xsltforms
android-c2dm
fay
floating-point-conversion
gora
jquery-data
source-highlighting
word-2010
domain-mapping
file-structure
applaud
working-directory
quilt
f2c
combinators
mkreversegeocoder
apache-config
project-settings
dynamic-websites
metaphone
graceful-degradation

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