ios - Hash Table Code Explanation -


i found implementation of hash table written in objective-c. can follow of it, struggling understand how -(id) init function works. method in hashtable.m file 3 lines (i repasted below right after question). explain doing? included of other relevant code although part think can follow rest. despite i'm unclear specifics of init method. thanks

-(id)init {     self =[super init];     self.othercontainer = [[nsmutablearray alloc]init];     return self; 

}

hashtable.h

#import <foundation/foundation.h>  @interface hashtable : nsobject  @property(nonatomic) nsmutablearray* othercontainer;  -(id)objectforkey:(nsstring*)name; -(void)setobject:(id)object forkey:(nsstring*)name; -(id)init;   @end 

hashtable.m

#import "hashtable.h" #import "person.h"  @implementation hashtable  -(id)init {     self =[super init];     self.othercontainer = [[nsmutablearray alloc]init];     return self; }  -(id)objectforkey:(nsstring*)name {     person* tempperson = nil;     (id item in self.othercontainer)     {         nsstring* tempname = [((person*)item) name];         if ([tempname isequaltostring:name])         {             tempperson = item;             break;         }      }       return tempperson; }  -(void)setobject:(id)object forkey:(nsstring*)name {     [self.othercontainer addobject:object]; }  @end 

part of viewcontroller.m

nsdata *data; nsfilehandle *fh; nsstring *inboundfile = @"/users/user/desktop/names.txt"; nsstring *filestring;      fh = [nsfilehandle filehandleforreadingatpath:inboundfile];      data = [fh readdatatoendoffile];     filestring = [[nsstring alloc]initwithdata:data encoding:nsutf8stringencoding];      nsarray *personarray = [filestring componentsseparatedbystring:@"\n"];      self.container = [[hashtable alloc]init];     (int x= 0; personarray.count > x ;x++) {         nsarray* tempnameandaddress = [personarray[x] componentsseparatedbystring:@" "];         person *persona = [[person alloc]init]; //could other ways of defining instance of object         persona.name = tempnameandaddress[0];         persona.address = tempnameandaddress[1];         if ([self.container objectforkey:persona.name] == nil)             [self.container setobject:persona forkey:persona.name];         else             nslog(@"%@ exists \n",persona.name);     } 

this right common init. self set object returned superclass init. miss 1 proper step. next step should if (self) { ...additional setup... } creating ivars/properties if self returned super init not nil. if self nil @ point bypass additional code , go straight return self. (returning nil)

the next line creating nsmutablearray ivar othercontainer property.

this not quite right. in init, when should use synthesized ivar directly. _othercontainer = [[nsmutablearray alloc] init];

nothing special here.


Comments

Popular posts from this blog

how to proxy from https to http with lighttpd -

android - Automated my builds -

python - Flask migration error -