ios - Confused on Document Based iPhone App Guide -
i'm writing 1 of first iphone apps - simple way of remembering things people. i'm following ios document called "document-based app programming guide ios" , i've gotten point query local device , icloud detect , load documents. in guide use custom class , since don't show code class, i'm confused on should like. give description of custom class "filerepresentation":
the example application has array (_filelist) of custom model objects encapsulate name , file url of each of application’s documents. (filerepresentation custom class of objects.)
can give example of "filerepresentation"'s class implementation like?
the parts listing 4-3 , 4-4 on page "managing lifecycle of document":
listing 4-3
-(void)viewdidload { [super viewdidload]; // set add , edit navigation items here.... if (self.documentsincloud) { _query = [[nsmetadataquery alloc] init]; [_query setsearchscopes:[nsarray arraywithobjects:nsmetadataqueryubiquitousdocumentsscope, nil]]; [_query setpredicate:[nspredicate predicatewithformat:@"%k '*.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]; (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]; (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]; } }
for people see in future, worked out seeing how other classes implement 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
Comments
Post a Comment