0

I have a @property (nonatomic,retain) NSMutableArray *transfer_array; in .h file and in my .m I have

@synthesize transfer_array = _transfer_array;

- (id)init
{
    self = [super init];
    if(self) {
        self.transfer_array = [[NSMutableArray alloc] init];
    }
    return self;
}

and I add objects to the array in this function

- (id)display:(double)imageXX andY:(double)imageYY withName:(NSString *)namee{

    //if((self == [super init])){
    NSLog(@"````````````````````````````````````````````````````````");

    NSLog(@"imageX: %f",imageXX);
    NSLog(@"imageY: %f", imageYY);
    NSLog(@"name: %@", namee);

    labelPoi = [[Poi alloc] init];
    labelPoi.imageLocationX = imageXX;
    labelPoi.imageLocationY = imageYY;
    labelPoi.name = namee;
    [self.transfer_array addObject:labelPoi];

The objects add successfully bu whenever I try to access the array elements in another instance such as:

- (void)viewDidLoad{
    [super viewDidLoad];

    NSLog(@"transfer_array count: %lu",(unsigned long)self.transfer_array.count);

Then the array is empty.

Any help would be appreciated!

4
  • 1
    You mean another instance of that class? The array is only usable in an instance. If you have two instances of that class you have two separate instances of that array. Commented Jul 30, 2013 at 17:52
  • Don't your viewDidLoad gets called first, and afterwards the - (id)display:(double)imageXX andY:(double)imageYY withName:(NSString *)namee{ function? Could you insert NSLog(@"transfer_array count: %lu",(unsigned long)self.transfer_array.count); in your display function, after you added the object to the array and show us the output? Commented Jul 30, 2013 at 17:56
  • I call display in another view controller before this view loads. Maybe I dont have my terminology correct, essentially when I attempt to use the array in another function, the array is empty Commented Jul 30, 2013 at 17:58
  • Also, I do have a NSLog(@"transfer_array count: %lu",(unsigned long)self.transfer_array.count); in the display function and the result is 2013-07-30 12:44:57.002 App[24164:11303] transfer_array count: 2 Commented Jul 30, 2013 at 17:59

1 Answer 1

3

Instances are independent objects. Changing the internal state of one doesn't affect the internal state of others.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.