在类目标c的另一个实例中访问数组

时间:2013-07-30 17:48:29

标签: ios objective-c arrays

我在.h文件中有一个@property (nonatomic,retain) NSMutableArray *transfer_array;,在我的.m中我有

@synthesize transfer_array = _transfer_array;

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

我在这个函数中添加了对象

- (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];

每当我尝试访问另一个实例中的数组元素时,对象就会成功添加,例如:

- (void)viewDidLoad{
    [super viewDidLoad];

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

然后数组为空。

任何帮助将不胜感激!

1 个答案:

答案 0 :(得分:3)

实例是独立的对象。改变一个人的内部状态不会影响他人的内部状态。