Question

I'm writing one of my first iPhone apps - a simple way of remembering things about people. I'm following the iOS document called "Document-Based App Programming Guide for iOS" and I've gotten to the point where we query the local device and iCloud to detect and load documents. In the guide they use a custom class and since they don't show the code for the class, I'm confused on what it should look like. They give this description of the custom class "FileRepresentation":

The example application now has an array (_fileList) of custom model objects that encapsulate the name and file URL of each of the application’s documents. (FileRepresentation is the custom class of those objects.)

Can anyone give an example of what "FileRepresentation"'s class implementation would look like?

The parts are Listing 4-3 and 4-4 on the page "Managing the Lifecycle of a Document":

Listing 4-3

-(void)viewDidLoad {
    [super viewDidLoad];
    // set up Add and Edit navigation items here....
    if (self.documentsInCloud) {
        _query = [[NSMetadataQuery alloc] init];
        [_query setSearchScopes:[NSArray arrayWithObjects:NSMetadataQueryUbiquitousDocumentsScope, nil]];
        [_query setPredicate:[NSPredicate predicateWithFormat:@"%K LIKE '*.txt'", NSMetadataItemFSNameKey]];
        NSNotificationCenter* notificationCenter = [NSNotificationCenter defaultCenter];
        [notificationCenter addObserver:self selector:@selector(fileListReceived)
            name:NSMetadataQueryDidFinishGatheringNotification object:nil];
        [notificationCenter addObserver:self selector:@selector(fileListReceived)
            name:NSMetadataQueryDidUpdateNotification object:nil];
        [_query startQuery];
    } else {
        NSArray* localDocuments = [[NSFileManager defaultManager] contentsOfDirectoryAtPath:
            [self.documentsDir path] error:nil];
        for (NSString* document in localDocuments) {
            [_fileList addObject:[[[FileRepresentation alloc] initWithFileName:[document lastPathComponent]
            url:[NSURL fileURLWithPath:[[self.documentsDir path]
            stringByAppendingPathComponent:document]]] autorelease]];
        }
    }
}

Listing 4-4

-(void)fileListReceived {
     NSString* selectedFileName=nil;
     NSInteger newSelectionRow = [self.tableView indexPathForSelectedRow].row;
     if (newSelectionRow != NSNotFound) {
        selectedFileName = [[_fileList objectAtIndex:newSelectionRow] fileName];
     }
     [_fileList removeAllObjects];
     NSArray* queryResults = [_query results];
     for (NSMetadataItem* result in queryResults) {
         NSString* fileName = [result valueForAttribute:NSMetadataItemFSNameKey];
         if (selectedFileName && [selectedFileName isEqualToString:fileName]) {
             newSelectionRow = [_fileList count];
         }
         [_fileList addObject:[[[FileRepresentation alloc] initWithFileName:fileName
             url:[result valueForAttribute:NSMetadataItemURLKey]] autorelease]];
     }
     [self.tableView reloadData];
     if (newSelectionRow != NSNotFound) {
         NSIndexPath* selectionPath = [NSIndexPath indexPathForRow:newSelectionRow inSection:0];
         [self.tableView selectRowAtIndexPath:selectionPath animated:NO scrollPosition:UITableViewScrollPositionNone];
     }
 }
Was it helpful?

Solution

For people that see this in the future, this is what I worked out by seeing how other classes implement their init functions:

#import "FileRepresentation.h"

@implementation FileRepresentation

@synthesize fileName = _fileName, fileUrl=_fileUrl;

-(id) initWithFileName:(NSString*)fileName url:(NSURL*)url
{
    self = [super init];
    if(self){
        self.fileName = fileName;
    }
    return self;
}

@end
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top