searching number in NSarray

Discussion in 'Mac Programming' started by andyiapan, Nov 27, 2010.

  1. macrumors newbie

    #1
    if i got number
    123456
    456786
    232133
    124456
    143445

    if i input 1,
    it will search and display out 123456,124456,143445
    if i input 12,
    it will search and display out 123456,124456
    if i keep on input 123,
    it will search and display out 123456

    how can i search like this?
     
  2. macrumors 6502a

    #2
    Easy to be hard

    I'm guessing you're asking for some low-level algorithm that compares your input to the array and returns the results you describe.

    I'd take a different tack and use the instance methods in the NSArray class. The idea is that the Cocoa designers have already thought of most situations programmers would need and have created the appropriate methods.

    Take a look at:
    http://developer.apple.com/library/...Foundation/Classes/NSArray_Class/NSArray.html

    probably start with one of the many indexOfObjectBlahBlah methods to see if they do what you want.
     
  3. macrumors newbie

    #3
    Thanks ~~~;)

     
  4. macrumors newbie

    #4
    i can't found out using compare,
    because of array value is not exactly same as compare value,

    how can i use NSarray and integer value compare in that way><

    Many Thanks

    1 = 12344
    1 = 1234455
    1 = 13433
    1 = 13333

    13 = 13333
    13 = 13433

    134 = 13433
     
  5. Moderator

    robbieduncan

    Staff Member

    #5
    Don't compare them as integers. Compare them as strings.
     
  6. macrumors newbie

    #6
    if i compare them with string, will i compare
    13 = 451322 because of 13 is same as 45(13)22.
     
  7. Moderator

    robbieduncan

    Staff Member

    #7
    That all depends on how you compare them. NSString has a very obvious method for checking if one string starts with another.
     
  8. macrumors 6502a

    #8
    Swim in the Cocoa Sea

    I agree with robbiedunan, who, as near as I can tell, is always right about everything.

    It appears that your (andyiapan) issue is that you're:
    1. Not familiar with Cocoa;
    2. Not familiar with using pre-existing methods.

    My advice is to use this current question as an opportunity to learn - open the NSArray documentation and just read through the various methods for 1/2 hour or an hour or until you get bored. Then do that a couple of times a day (expanding past NSArray to NSString or other classes) for a week or two.

    Then the next time you have a question, instead of asking "what algorithm can I implement to get my answer", you'll ask "what methods are available that do what I want or almost what I want"?
     

Share This Page