Advertise Mobile SDKs Books Events Forum News Social Networking Support Us
Follow @iphonedevsdk on Twitter

Mockup & CodeGen, iPhone & iPad
($9.99)

Make your own iPhone apps
and run them live!
(free)

Manu
($0.99)

Want your application or service advertised on iPhone Dev SDK?

Go Back   iPhone Dev SDK Forum > iPhone SDK Development Forums > iPhone SDK Tools & Utilities

Reply
 
LinkBack Thread Tools Display Modes
Old 12-09-2009, 04:12 AM   #1 (permalink)
Registered Member
 
Join Date: Dec 2009
Posts: 4
Default Problem with NSString compare

Hello all,

It probably seems to be a noob question for a lot of members, but I looked for this on the forum and I was unable to find an answer :
how can I check in a string contains another string ?

I tried something like this :

Quote:
if ([item.title compare:searchBar.text]==NSOrderedSame) || ([item.subtitle compare:searchBar.text]==NSOrderedSame)
But the problem is this always returns TRUE

I looked in the Xcode doc too, but either I don't completely understand it or I'm doing something wrong but I still cannot get the result I want.

I'm trying to use this to check if the text typed in a searchBar corresponds to the title or subtitle attributes of an objects array.

Could someone help me on this ?

Thanks
keyneosoft is offline   Reply With Quote
Old 12-09-2009, 04:51 AM   #2 (permalink)
Maker of Games
 
Mr Jack's Avatar
 
Join Date: Nov 2009
Location: Coventry, UK
Posts: 395
Default

Firstly, please post the exact code you're using rather than "something like" it.

Secondly, are you checking that all the strings you are referencing are valid? In particularly that none of them are nil references?
__________________


Visit Mr Jack Games for my blog and more about my games
Mr Jack is offline   Reply With Quote
Old 12-09-2009, 04:51 AM   #3 (permalink)
Registered Member
 
Join Date: Dec 2009
Posts: 4
Default

Quote:
Firstly, please post the exact code you're using rather than "something like" it.

Secondly, are you checking that all the strings you are referencing are valid? In particularly that none of them are nil references?
Thanks to you that works. Indeed that's because my item.subtitle are nil actually.
So I ask 1 more question : why did that return yes if I was looking for "aText" in a nil string ?

Thanks for your answer

Last edited by keyneosoft; 12-09-2009 at 05:03 AM.
keyneosoft is offline   Reply With Quote
Old 12-09-2009, 05:55 AM   #4 (permalink)
Maker of Games
 
Mr Jack's Avatar
 
Join Date: Nov 2009
Location: Coventry, UK
Posts: 395
Default

Quote:
Originally Posted by keyneosoft View Post
So I ask 1 more question : why did that return yes if I was looking for "aText" in a nil string ?
If you check the docs you'll see that behaviour during a comparison with a 'nil' is "undefined", so it's just how it happens to be rather than a reliable design decision.
__________________


Visit Mr Jack Games for my blog and more about my games
Mr Jack is offline   Reply With Quote
Old 12-09-2009, 06:26 AM   #5 (permalink)
Registered Member
 
Join Date: Nov 2009
Location: London
Posts: 226
Default

Quote:
Originally Posted by keyneosoft View Post
So I ask 1 more question : why did that return yes if I was looking for "aText" in a nil string ?
If you send a message to a nil object (in this case, item.subtitle) you get a nil returned. And since nil is basically 0, and NSOrderedSame is defined as 0, the == test returns true.
__________________
SimCap - Simple iPhone and iPad Simulator screen capture
_sjc_ is offline   Reply With Quote
Old 12-09-2009, 07:08 AM   #6 (permalink)
Maker of Games
 
Mr Jack's Avatar
 
Join Date: Nov 2009
Location: Coventry, UK
Posts: 395
Default

Quote:
Originally Posted by _sjc_ View Post
If you send a message to a nil object (in this case, item.subtitle) you get a nil returned. And since nil is basically 0, and NSOrderedSame is defined as 0, the == test returns true.
Ah yes, not reading correctly I'd though he was passing a nil object as the compare argument.
__________________


Visit Mr Jack Games for my blog and more about my games
Mr Jack is offline   Reply With Quote
Old 12-09-2009, 08:11 AM   #7 (permalink)
Registered Member
 
Join Date: Dec 2009
Posts: 4
Default

Quote:
Originally Posted by Mr Jack View Post
Ah yes, not reading correctly I'd though he was passing a nil object as the compare argument.
Ok thanks for this explanation.
I have 1 more question :
My comparison now almost works correctly. But I have a problem with it, as it seems to be case-sensitive. I tried to use

Quote:
if([item.title caseInsensitiveCompare:searchBar.text]==NSOrderedSame)
But it seems to return false if the string I type in the searchBar is not exactly the same as the item.title.

Maybe you can help me again for this.
Thank you
keyneosoft is offline   Reply With Quote
Old 12-09-2009, 11:41 AM   #8 (permalink)
Maker of Games
 
Mr Jack's Avatar
 
Join Date: Nov 2009
Location: Coventry, UK
Posts: 395
Default

Quote:
Originally Posted by keyneosoft View Post
But it seems to return false if the string I type in the searchBar is not exactly the same as the item.title.
You're French, no? Are you comparing strings that contain accented characters? These may not be correctly compared unless you use localizedCaseInsensitiveCompare instead.

Otherwise, could you post your code, and the specific strings that you're having problems with?
__________________


Visit Mr Jack Games for my blog and more about my games
Mr Jack is offline   Reply With Quote
Old 12-09-2009, 12:05 PM   #9 (permalink)
Registered Member
 
Join Date: Dec 2009
Posts: 4
Default

Quote:
Originally Posted by Mr Jack View Post
You're French, no? Are you comparing strings that contain accented characters? These may not be correctly compared unless you use localizedCaseInsensitiveCompare instead.

Otherwise, could you post your code, and the specific strings that you're having problems with?
Yes I'm french, but I think that doesn't come from this.
I cannot post the specific strings because they come from a csv of french towns that I parse.
The strings are composed an address (including the name of the city) : for instance 24 RUE DE LA LIBERTE LILLE and so on.
Every string is in uppercase, to avoid the accented characters case.

On the app, I want to find the objects that correspond to a search I'd type in the searchBar.

My test is the following one :

Quote:
for (POI *item in allPOIs) {
int index = 0;
if([searchBar.text isEqualToString:item.title] || [searchBar.text isEqualToString:item.subtitle]
|| [item.title hasPrefix:searchBar.text] || [item.subtitle hasPrefix:searchBar.text]
|| [item.title hasSuffix:searchBar.text] || [item.subtitle hasSuffix:searchBar.text]
|| ([item.title caseInsensitiveCompare:searchBar.text]==NSOrderedSame))
{
[searchTempPOIs addObject:item];
}
index++;
}
where searchTempPOIs is an array of POI.
Do you see where is my mistake ?
Thanks for your answers anyway
keyneosoft is offline   Reply With Quote
Old 12-09-2009, 12:35 PM   #10 (permalink)
Maker of Games
 
Mr Jack's Avatar
 
Join Date: Nov 2009
Location: Coventry, UK
Posts: 395
Default

Quote:
Originally Posted by keyneosoft View Post
The strings are composed an address (including the name of the city) : for instance 24 RUE DE LA LIBERTE LILLE and so on.
Every string is in uppercase, to avoid the accented characters case.

On the app, I want to find the objects that correspond to a search I'd type in the searchBar.
Hang on, you're making a search bar? Are you looking for exact comparisons or substrings? The code you've got should match '24 rue de la liberte lille' for your example, but it won't match 'rue' or 'rue de la liberte'.

Have a look at rangeOfString: instead, it will search for substring matches anywhere in a string.

Even so, this is a very inefficient way of searching, unless you have a small search space you may find it gets impractically slow.
__________________


Visit Mr Jack Games for my blog and more about my games
Mr Jack is offline   Reply With Quote
Reply

Bookmarks

Tags
comparison, nsstring

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On



» Advertisements
» Online Users: 313
19 members and 294 guests
@sandris, ADY, dacapo, Dani77, djohnson, dre, HDshot, HemiMG, JasonR, MarkC, mer10, nibeck, prchn4christ, ryandb2, spiderguy84, timle8n1, tomtom100, vogueestylee
Most users ever online was 1,187, 10-11-2011 at 08:09 AM.
» Stats
Members: 158,882
Threads: 89,229
Posts: 380,763
Top Poster: BrianSlick (7,129)
Welcome to our newest member, jansan
Powered by vBadvanced CMPS v3.1.0

All times are GMT -5. The time now is 02:04 PM.
Powered by vBulletin® Version 3.8.0
Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.3.0