Trim Array Path Down to Only Filename

Discussion in 'iOS Programming' started by newtoiphonesdk, Apr 17, 2012.

  1. newtoiphonesdk macrumors 6502a

    Joined:
    Jul 30, 2010
    #1
    My NSArray is:

    Code:
    self.files  = [bundle pathsForResourcesOfType:@"ppt" inDirectory:@"thepowerpoints"];
    
    This returns the full path of /User/Name/.../filename.ppt

    I can use a NSString to get only the filename using:

    Code:
    NSString *documentsDirectoryPath = [self.files objectAtIndex:thepath.row];
    self.filenames = [[documentsDirectoryPath lastPathComponent] stringByDeletingPathExtension];
    Is there a way that I can cleanup the original NSArray to only return the lastPathComponent without the extension?

    Am needing to do this to search within the tableview array. Currently, if a file is named "Test.ppt" the search bar will show it if I type in "/User/Name"

    I would prefer for it to only search the filename.
     
  2. chown33 macrumors 604

    Joined:
    Aug 9, 2009
    #2
    How would you usually change one of the elements of an NSArray? Do the same thing here. The strategy doesn't change just because you have a different array or contents.

    Since NSArray is immutable, you'll basically do the following:
    1. Make a mutable copy of the original array.
    2. Get the element you wish to change.
    3. Change the object.
    4. Replace the original element in the array with the changed object.

    The result should be an NSArray with one element changed.


    This is essentially the same answer I gave in your other thread:
    http://forums.macrumors.com/showthread.php?p=14691134#post14691134

    The short answer: get the element you want to change, change it, replace the element in the array with the changed object.
     
  3. MattInOz macrumors 68030

    MattInOz

    Joined:
    Jan 19, 2006
    Location:
    Sydney
    #3
    Might be worth a look at NSURL objects to store your file paths.
    I recall it's last component method drops the extension.
     
  4. newtoiphonesdk thread starter macrumors 6502a

    Joined:
    Jul 30, 2010
    #4
    Ok, I got that accomplished:
    Code:
    NSMutableArray *names = [NSMutableArray arrayWithCapacity:[self.files count]];
        for (NSString *path in self.files) {
            [names addObject:[[path lastPathComponent] stringByDeletingPathExtension]];
        }
        self.files = names;
    But it only creates the problem of now the path is not part of the array, and won't be loaded into the webview on didSelectRow
     
  5. chown33 macrumors 604

    Joined:
    Aug 9, 2009
    #5
    Please describe your model. That's the M in MVC: Model, View, Controller. If there isn't a clear model, then you should probably work on clarifying that.

    You should be able to explain what's in the files array (property), the filenames array (property), and how those relate to the overall model.

    You should also be able to describe what each item of your table represents, and how that does or doesn't relate to the files array and the filenames array.

    If one of your arrays or table items represents a single file, then say it represents a single file. If something represents a collection of files, such as an array holding collection of PPT files, then say it represents a collection. If an array only represents the components of a pathname, then say that.

    I have no clear idea of what you're trying to represent, what the model is (or is trying to represent), nor what you're trying to accomplish.
     
  6. newtoiphonesdk thread starter macrumors 6502a

    Joined:
    Jul 30, 2010
    #6
    Each row of the Table View represents one ppt file. The array *files is all of the ppt files in the directory, thepowerpoints.

    Code:
        self.files  = [bundle pathsForResourcesOfType:@"ppt" inDirectory:@"thepowerpoints"];
    
    When accessed at indexPath.row, it is the entire path of the file, which works great when selecting the row to load the ppt into a webview. But, when searching, if you type anything that is in the path of the file (i.e. /User/Name/...) it will show every file. I tried cleaning up the array to only return the file name:

    Code:
    NSMutableArray *names = [NSMutableArray arrayWithCapacity:[self.files count]];
        for (NSString *path in self.files) {
            [names addObject:[[path lastPathComponent] stringByDeletingPathExtension]];
        }
        self.files = names;
    This helps with the search issue, as now it will only show the file name and not the path, but when trying to select one, since the path is no longer there, it will not show in the webview.

    Does that help explain?
     
  7. chown33 macrumors 604

    Joined:
    Aug 9, 2009
    #7
    Yes, that clarifies things.

    You need to change how you search, or change how you hold the objects. You haven't posted any search code, so I can't say what to change there.

    What the search needs to do is not search the entire path of the ppt file. One way to do that is do perform the search by taking the last component of the pathname, then only doing the search test on that component. There are several ways to accomplish that: blocks and indexOfObjectPassingTest: is one.

    Another way is to create a simple data-object class with two properties: pathname and filename. Search only searches the filename, while the webview gets only the full pathname.

    Another way is to keep two parallel arrays, one that has the whole pathname, the other has only the filename. Search only searches the filename array, while the webview only gets elements from the pathname array.

    In short, define a data structure (a Model) that exposes only the necessary data (pathname or filename) to the code that operates on it (the search or the webview).
     
  8. newtoiphonesdk thread starter macrumors 6502a

    Joined:
    Jul 30, 2010
    #8
    I am using the search from a tutorial that someone pointed out to me. The search part is:

    Code:
    - (void)handleSearchForTerm:(NSString *)searchTerm
    {
        [self setSavedSearchTerm:searchTerm];
    	
        if ([self searchResults] == nil)
        {
            NSMutableArray *array = [[NSMutableArray alloc] init];
            [self setSearchResults:array];
            [array release], array = nil;
        }
    	
        [[self searchResults] removeAllObjects];
    	
        if ([[self savedSearchTerm] length] != 0)
        {
            for (NSString *currentString in [self files])
            {
                if ([currentString rangeOfString:searchTerm options:NSCaseInsensitiveSearch].location != NSNotFound)
                {
                    [[self searchResults] addObject:currentString];
                }
            }
        }
    }
    
     
  9. chown33 macrumors 604

    Joined:
    Aug 9, 2009
    #9
    Do you understand what the code is doing? Or did you just copy it without understanding it?

    I've hilited the search-test in red. That's the part that determines whether some candidate (i.e. currentString) should be in the search results (searchResults) or not. All searching ultimately breaks down to testing candidates and keeping results. "Is this the droid string I'm looking for?" "How about this one?" "This one?"

    If you understand what the test is doing, then replace it with a test that does what you want. That is, a test that only searches the last component of the pathname string. If you don't know how to do that, break it down. Figure out which part of the string to search. Make a variable that contains only that part. Do the test on only that variable. Keep the result (or don't keep it) depending on the outcome of the test.
     

Share This Page