Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

macman7002

macrumors regular
Original poster
Apr 28, 2008
100
0
I have looked around online for help with this, but I can't seem to find anything. So it makes me think I'm either missing something or not looking for it the right place, that's why I'm bringing it here.

I would like to compare strings in ObjC and ignore the case of the letters in each string. I have comparing the strings down, but I can't figure out how to compare them while ignoring case.

For example, getting "Hello" = "hello"
or "hello" = "Hello"

One string comes from user input via textfield. I have that set-up like this:

NSString *fieldOfText = fieldText.text;

The other string comes from the "if" statement. I have that set-up like this:

if ( [fieldOfText isEqualToString:mad:"hello"])
{
doSomething
}
else if ( ![fieldOfText isEqualToString:mad:"hello"])
{
doSomethingLikeDisplayAlert
}

What would be the best way to ignore/compare the strings while ignoring cases?

Thanks for reading, and thanks in advance!
 

macman7002

macrumors regular
Original poster
Apr 28, 2008
100
0
Did you open the documentation and look at the comparison methods? If so any reason you're not using the method designed exactly for this?

caseInsensitiveCompare:

I did, and I sort of skimmed over that because I couldn't figure out how to implement that under my buttonPressed method that contains my "if" and "else if". Any chance you might be able to give me an example of how that works? I am still warming up to Objective-C and Cocoa even though I have used Macs for a long time.
 

robbieduncan

Moderator emeritus
Jul 24, 2002
25,611
893
Harrogate
Search for NSComparisonResult which is what that method returns. You can see the documentation here (search on the page). So you simply have to check for equality of the return from the function and the constant you want (NSOrderedSame).
 

robbieduncan

Moderator emeritus
Jul 24, 2002
25,611
893
Harrogate
As I mentioned in my last post, I am still new to Objective-C and was not aware of that method.

You don't need to be aware of the methods: use the documentation. No-one can possibly memorise the entire API. We are all opening the documentationd and reading it all the time. This is a core skill. It must be learnt.
 

macman7002

macrumors regular
Original poster
Apr 28, 2008
100
0
Memorizing the entire API would be a bit daunting. I guess what I should have said is I'm still really new to developing on the Mac for Mac and iPhone and all the resources available to me. And yes, I do need to get used to look at Apple's documentation and working through it, but sometimes it overwhelms me.

I'm still not sure I understand how I would use the localizedCaseInsensitiveCompare in context of my app.

Would I put that in my "if" statement in my -(void)buttonPressed {} ?

Thank you for the help so far!
 

robbieduncan

Moderator emeritus
Jul 24, 2002
25,611
893
Harrogate
I'm not about to write any code. I don't do that: I simply don't think it helps anyone. The method returns a value. You want to compare that with another value that you have (the constant that I already told you about). You compare one value with another value. The values are not objects. They are, in fact, integers (which is what the documentation tells you). So you are comparing one integer with another integer. Can you check if one integer is the same as another? If so you already know what to do.
 

macman7002

macrumors regular
Original poster
Apr 28, 2008
100
0
I'm not about to write any code. I don't do that: I simply don't think it helps anyone. The method returns a value. You want to compare that with another value that you have (the constant that I already told you about). You compare one value with another value. The values are not objects. They are, in fact, integers (which is what the documentation tells you). So you are comparing one integer with another integer. Can you check if one integer is the same as another? If so you already know what to do.

I'm sorry if I am sound like I am asking you to write code, because that's not what I am asking for, for the same reason you told me you wouldn't write any code here.

So far, I understand that the method returns an integer value, and I believe I know how to compare integers. And I think I just figured out how to do it! Would you mind if I post that portion of my code for review?
 

macman7002

macrumors regular
Original poster
Apr 28, 2008
100
0
Thank you!

Thank you MikhailT and robbieduncan!!!
I have my app running the way I need it to run now. And I also learned to use Apple's documentation and not get overwhelmed by it, but to work with it and take it one step at a time.

Thank you robbieduncan for working through this with me!
 

robbieduncan

Moderator emeritus
Jul 24, 2002
25,611
893
Harrogate
Thank you MikhailT and robbieduncan!!!
I have my app running the way I need it to run now. And I also learned to use Apple's documentation and not get overwhelmed by it, but to work with it and take it one step at a time.

Thank you robbieduncan for working through this with me!

Great! Sorry I did not respond to your post above: I was asleep :)
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.