Comparing Case Insensitive Strings in ObjC

Discussion in 'iOS Programming' started by macman7002, May 7, 2010.

  1. macman7002 macrumors regular

    Joined:
    Apr 28, 2008
    #1
    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!
     
  2. robbieduncan Moderator emeritus

    robbieduncan

    Joined:
    Jul 24, 2002
    Location:
    London
    #2
    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:
     
  3. MikhailT macrumors 601

    Joined:
    Nov 12, 2007
    #3
    Why don't you call lowercase method on both of them before comparing them?
     
  4. macman7002 thread starter macrumors regular

    Joined:
    Apr 28, 2008
    #4
    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.
     
  5. robbieduncan Moderator emeritus

    robbieduncan

    Joined:
    Jul 24, 2002
    Location:
    London
    #5
    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).
     
  6. macman7002 thread starter macrumors regular

    Joined:
    Apr 28, 2008
    #6
    As I mentioned in my last post, I am still new to Objective-C and was not aware of that method.
     
  7. robbieduncan Moderator emeritus

    robbieduncan

    Joined:
    Jul 24, 2002
    Location:
    London
    #7
    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.
     
  8. macman7002 thread starter macrumors regular

    Joined:
    Apr 28, 2008
    #8
    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!
     
  9. robbieduncan Moderator emeritus

    robbieduncan

    Joined:
    Jul 24, 2002
    Location:
    London
    #9
    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.
     
  10. macman7002 thread starter macrumors regular

    Joined:
    Apr 28, 2008
    #10
    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?
     
  11. macman7002 thread starter macrumors regular

    Joined:
    Apr 28, 2008
    #11
    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!
     
  12. robbieduncan Moderator emeritus

    robbieduncan

    Joined:
    Jul 24, 2002
    Location:
    London
    #12
    Great! Sorry I did not respond to your post above: I was asleep :)
     

Share This Page