Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

So I know my problem is that my transfer array is not being initialized correctly. Where should I 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];
    // Do any additional setup after loading the view from its nib.

    NSLog(@"transfer count: %lu",(unsigned long)transfer.count);
    for(int i = 0; i < 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 any resources that can be 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 is made up of an imageLocationX, imageLocationY, and name and I am trying to put the Poi object into an array named transfer however, whenever I try to access transfer elements, I receive 0 or null. The (id)display function is being called several times from a different view NSMutable alloc in that function, the array gets reset.

Here is the 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
share|improve this question
    
put the transfer = [[NSMutableArray alloc] init]; after [super viewDidLoad]; –  Moy Jul 19 '13 at 16:32
3  
Where do you declare the variable transfer? I have never seen @synthesize without @property, so not sure what the behavior is for this. –  Gustav Larsson Jul 19 '13 at 16:37
    
Please, add your header code, because even if there is a property transfer, you should access it by _transfer. –  Dominik Hadl Jul 19 '13 at 16:42
    
It's perfectly fine to do the init of transfer where you have it, but your initWithLabel should be calling [self init], not [super init]. –  Hot Licks Jul 19 '13 at 18:23
    
putting transfer = [[NSMutableArray alloc] init]; after the [super viewDidLoad] will not working because the objects are put into the array before the screen is loaded. I edited my question to include the header file. –  user2125844 Jul 19 '13 at 19:41

4 Answers 4

I guess one problem can be that when you are creating this controller you are calling to:

 -(id)initWithLabel:(double)imageX andY:(double)imageY withName:(NSString *)name

In case you do that, you are calling to [super init] what will never go through the init method of your class (where you allocated the array). In case you have different methods to init a controller I recommend you to have a:

commonInit{
   _transfer = [[NSMutableArray] alloc] init];
}

Then call this method for every init method that you have in your controller so you ensure that array is allocated. Other thing is just allocate your array in the viewDidLoad of your controller.

Just for making know, you can "allocated" an array without having to take care about release the object by calling to [NSMutableArray arrayWithCapacity:0];

share|improve this answer

Are you initializing your PictureViewController with -(id)initWithLabel:(double)imageX andY:(double)imageY withName:(NSString *)name? If so, I can tell you what the problem is...

-(id)initWithLabel:(double)imageX andY:(double)imageY withName:(NSString *)name
{
    self = [super init];   // <--- You are likely confused about this line

    .....
}

[super init] does not pass a message to your custom -(id)init method. It instead is referring to PictureViewController's superclass, which is likely UIViewController.

Your problem should be fixed by uncommenting transfer = [[NSMutableArray alloc] init]; in the initWithLabel: custom init method.

share|improve this answer
    
initWithLabel should be doing [self init], not [super init]. –  Hot Licks Jul 19 '13 at 18:24
    
Yes, that just about summarizes my post haha. Or, just moving the code. –  MikeS Jul 19 '13 at 18:58
    
Fixed this but its still not working :/ –  user2125844 Jul 19 '13 at 19:54

This init method should become the "designated" initializer:

-(id)initWithLabel:andY:withName:(NSString *)name

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

The designated initializer shall initialize the instance properly (that is, in your case it may initialize the array transfer, unless you use a lazy accessor ).

The "designated initializer" is most often the "most specialized" initializer - that is, that one with the most parameters. Most often, there is only one and easily identifiable designated initializer.

The "designated initializer" has the canonical form:

-(id)initWithLabel:(double)imageX andY:(double)imageY withName:(NSString *)name{
    self = [super init];
    if(self){
        // initialization
        ...
    }
}

Other init methods like the init method shall invoke the designated initializer like:

- (id)init {
    return [self initWithLabel:0 andY:0 withName:@""];
}
share|improve this answer

make a property (synthesize it @synthesize transfer=_transfer;), use lazy instantiation like:

-(NSMutableArray*)transfer
{
   if (!_transfer)
   {
       _transfer=[NSMutableArray array];
   }

   return _transfer;
}

and take it out of your init

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.