ios - Trying to initialize array objective -c -


so know problem transfer array not being initialized correctly. should put transfer = [[nsmutablearray alloc] init];?

@interface pictureviewcontroller (){      poi *labelpoi;  }  @end  @implementation pictureviewcontroller @synthesize imagex; @synthesize imagey; @synthesize name; @synthesize transfer;   - (id)init {     self = [super init];     if(self) {         transfer = [[nsmutablearray alloc] init];     }     return self; } -(id)initwithlabel:(double)imagex andy:(double)imagey withname:(nsstring *)name{     self = [super init];     if(self){         // transfer = [[nsmutablearray alloc] init];          self.imagex = imagex;         self.imagey = imagey;         self.name = name;     }      return self; }   /*-(id)initwithnibname:(nsstring *)nibnameornil bundle:(nsbundle *)nibbundleornil{     self = [super initwithnibname:nibnameornil bundle:nibbundleornil];      if(self){          transfer = [[nsmutablearray alloc] init];          self.imagex = imagex;         self.imagey = imagey;         self.name = name;         nslog(@"imagex: %f", self.imagex);         nslog(@"imagey: %f", imagey);         nslog(@"name: %@", name);      }     return self; }*/  - (void)viewdidload {     [super viewdidload];     // additional setup after loading view nib.      nslog(@"transfer count: %lu",(unsigned long)transfer.count);     for(int = 0; < transfer.count; i++){         uilabel *label =  [[uilabel alloc] initwithframe: cgrectmake([[transfer objectatindex:i] imagelocationx], [[transfer objectatindex:i] imagelocationy], 200, 50)];         label.text = [[transfer objectatindex:i] name];         label.font = [uifont systemfontofsize:14];         label.backgroundcolor = [uicolor colorwithred:0.0 green:0.0 blue:0.0 alpha:0.0];         [self.view addsubview:label];         nslog(@"asdfasdsd");     } }   - (void)didreceivememorywarning {     [super didreceivememorywarning];     // dispose of resources can recreated. }    - (id)display:(double)imagexx andy:(double)imageyy withname:(nsstring *)namee{     nslog(@"%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%");     nslog(@"imagex: %f",imagexx);     nslog(@"imagey: %f", imageyy);     nslog(@"name: %@", namee);      labelpoi = [[poi alloc] init];     //transfer = [[nsmutablearray alloc] init];     labelpoi.imagelocationx = imagexx;     labelpoi.imagelocationy = imageyy;     labelpoi.name = namee;     [transfer addobject:labelpoi];     nslog(@"label.x: %f should be: %f", labelpoi.imagelocationx, imagexx);     nslog(@"label.y: %f should be: %f", labelpoi.imagelocationy, imageyy);     nslog(@"label.name: %@ should be: %@",labelpoi.name,namee);     nslog(@"transssssfer: %lu", (unsigned long)transfer.count);      nslog(@"asfddfsaasfdfdsfsd %f", [[transfer objectatindex:0] imagelocationx]);      return self; }  @end 

the poi object made of imagelocationx, imagelocationy, , name , trying put poi object array named transfer however, whenever try access transfer elements, receive 0 or null. (id)display function being called several times different view nsmutable alloc in function, array gets reset.

here output:

2013-07-19 11:22:06.736 ar_uav_app[12466:11303] %%%%%%%%%%%%%%%%%%%%%%%% 2013-07-19 11:22:06.736 ar_uav_app[12466:11303] imagex: 224.485718 2013-07-19 11:22:06.736 ar_uav_app[12466:11303] imagey: 116.353401 2013-07-19 11:22:06.736 ar_uav_app[12466:11303] name: beutel student health center 2013-07-19 11:22:06.736 ar_uav_app[12466:11303] label.x: 224.485718 should be: 224.485718 2013-07-19 11:22:06.736 ar_uav_app[12466:11303] label.y: 116.353401 should be: 116.353401 2013-07-19 11:22:06.736 ar_uav_app[12466:11303] label.name: beutel student health center should be: beutel student health center 2013-07-19 11:22:06.737 ar_uav_app[12466:11303] transssssfer: 0 2013-07-19 11:22:06.737 ar_uav_app[12466:11303] asfddfsaasfdfdsfsd 0.000000 2013-07-19 11:22:06.737 ar_uav_app[12466:11303] ############################################################# 

edit: .h file

@interface pictureviewcontroller : uiviewcontroller{     nsmutablearray *transfer; }  @property (nonatomic) double imagex; @property (nonatomic) double imagey; @property (nonatomic) nsstring *name;  @property (nonatomic,retain) nsarray *transfer; - (ibaction)backview:(id)sender; - (ibaction)load:(id)sender x:(double)imagex andy:(double)imagey withname:(nsstring *)name;  -(id)initwithlabel:(double)imagex andy:(double)imagey withname:(nsstring *)name; -(id)display:(double)imagex andy:(double)imagey withname:(nsstring *)name; @end 

this init method should become "designated" initializer:

-(id)initwithlabel:andy:withname:(nsstring *)name

(by way, it's not named correctly according naming conventions)

the designated initializer shall initialize instance (that is, in case may initialize array transfer, unless use lazy accessor ).

the "designated initializer" "most specialized" initializer - is, 1 most parameters. often, there only one , identifiable designated initializer.

the "designated initializer" has canonical form:

-(id)initwithlabel:(double)imagex andy:(double)imagey withname:(nsstring *)name{     self = [super init];     if(self){         // initialization         ...     } } 

other init methods init method shall invoke designated initializer like:

- (id)init {     return [self initwithlabel:0 andy:0 withname:@""]; } 

Comments