Searching Directory for items

Discussion in 'Mac Programming' started by larswik, Jan 8, 2013.

  1. larswik macrumors 68000

    Sep 8, 2006
    I have a little Mac program that I wrote a while back to help me manage a small data base of graphics and info for a local grocery store. The data base has grown so large that it is hard for me to find things and the items that get sent to me become hard to look up just scanning through the directory. 1 entry might be "Wine Firestone" and another might be "Sunstone Wine". IF I look under "W" for wine I would miss sunstone.

    So I thought I would add a search bar to my program which I have never done before. My goal is to enter a search word like wine and every entry that has the word "wine" in it would be displayed.

    I was going to use NSFileManager to get a list of the items in a directory and in to an array. Then I was going to check each index to see if there is a sub string that matches the string that I am looking for. If there is a match add it to a new mutableArray. After the search display in a tableview.

    Is that about the correct process to do this?
  2. KoolStar macrumors demi-god


    Oct 16, 2006
    I believe that would be the most common approach. I would use the nsrange and use its range function for a string if it exist then they text contains the string you are checking for.
  3. ytk macrumors regular

    Jul 8, 2010
    Just make sure your comparison isn't case-sensitive. One way to do this is to convert both your search and filename strings to upper or lower case before comparison, using NSString's upperCase or lowerCase methods.
  4. subsonix macrumors 68040

    Feb 2, 2008
    Just to see if I understand, your items are files in a folder? Your program looks for a string match (the search term) to a file name?
  5. larswik thread starter macrumors 68000

    Sep 8, 2006
    subsonix - Yes. I need a way to filter the data which will be string.
    ytk - I forgot about that, good point.
    KoolStar- Ok, so this is the approach. Objective C is so vast that I was not sure if some special class existed to do this. I was wondering if some one was going to mention a class that I was unaware of that helped it out. But it seems that this approach is the right way.


Share This Page