Objective-C中的数组初始化问题

时间:2013-07-29 18:29:19

标签: ios objective-c arrays

我遇到从一个视图控制器输入信息到另一个视图控制器的问题。此函数位于第二个视图控制器中,并且在第一个控制器中被多次调用,因为它在for循环中,并且我想要将多个对象添加到数组transfer_array中。对象的类型为Poi,由imageLocationX,imageLocationY和name组成。我有BOOL的原因是我只尝试初始化数组一次。 Poi对象在第一次调用函数时添加到数组中,并且在再次调用函数后数组为空。

- (id)display:(double)imageXX andY:(double)imageYY withName:(NSString *)namee ifDone:(BOOL *)donee{
    NSLog(@"````````````````````````````````````````````````````````");

    NSLog(donee ? @"Yes" : @"No");
    if(donee == NO){
        transfer_array = [[NSMutableArray alloc] init];
    }

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

    labelPoi = [[Poi alloc] init];
    labelPoi.imageLocationX = imageXX;
    labelPoi.imageLocationY = imageYY;
    labelPoi.name = namee;
    [transfer_array 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_array.count);

    return self;
}

以下是第一次调用函数时的日志:

````````````````````````````````````````````````````````
2013-07-29 13:25:52.502 App[20856:11303] No
2013-07-29 13:25:52.502 App[20856:11303] imageX: 979.008057
2013-07-29 13:25:52.503 App[20856:11303] imageY: 115.728180
2013-07-29 13:25:52.503 App[20856:11303] name: Urgent Care
2013-07-29 13:25:52.503 App[20856:11303] label.x: 979.008057 should be: 979.008057
2013-07-29 13:25:52.503 App[20856:11303] label.y: 115.728180 should be: 115.728180
2013-07-29 13:25:52.503 App[20856:11303] label.name: Urgent Care should be:  Urgent Care
2013-07-29 13:25:52.503 App[20856:11303] transfer_array.count: 1

第二次:

2013-07-29 13:25:52.506 App[20856:11303] ````````````````````````````````````````````````````````
2013-07-29 13:25:52.506 App[20856:11303] Yes
2013-07-29 13:25:52.506 App[20856:11303] imageX: 224.485718
2013-07-29 13:25:52.506 App[20856:11303] imageY: 116.353401
2013-07-29 13:25:52.506 App[20856:11303] name: Student Health Center
2013-07-29 13:25:52.507 App[20856:11303] label.x: 224.485718 should be: 224.485718
2013-07-29 13:25:52.507 App[20856:11303] label.y: 116.353401 should be: 116.353401
2013-07-29 13:25:52.507 App[20856:11303] label.name: Student Health Center should be:  Student Health Center
2013-07-29 13:25:52.507 App[20856:11303] transfer_array.count: 0

我无法访问数组中的任何信息,因为它是空的。有谁知道如何修改这个,以便该函数将不断添加我想要添加的对象而不是保持为空?

编辑

这就是在第一个视图控制器中调用该函数的方式,这个方法是由朋友向我推荐的

PictureViewController *newlabel = [[PictureViewController alloc] display:PointOfInterest.imageLocationX andY:PointOfInterest.imageLocationY withName:PointOfInterest.name ifDone:done];
            if(done == NO){
                done = YES;
            }

2 个答案:

答案 0 :(得分:1)

虽然esker的答案确实消除了已完成的必要性,因此更可取,但这不是您的错误所在的位置。您遇到的问题是,每次进行循环时,您都在初始化PictureViewController的新实例。在这个新对象中,尚未创建数组,因此它是(null),返回0。

您需要做的是创建viewController的一个实例,然后将所有对象添加到它。此外,在init方法中,您要调用[super init]或其他版本的超级初始化程序或另一个调用超级初始化程序的初始化程序。

答案 1 :(得分:1)

叹!!完全删除

NSLog(donee ? @"Yes" : @"No");
if(donee == NO){
    transfer_array = [[NSMutableArray alloc] init];
}

如果您已添加,请完全删除它:

// property getter for transfer_array: lazily load the object
- (NSArray *)transfer_array
{
    return (transfer_array ?: transfer_array = [[NSMutableArray alloc] init];
}

添加:

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

-(void)dealloc {
   self.transfer_array = nil;
   // If you are not using ARC include this line
   [super dealloc];
   // (If you're not sure, add it and then remove it if the compiler complains about it.)
}

更改" transfer_array"的所有剩余事件to" self.transfer_array"。