将多个对象添加到NSMutableArray时遇到问题

时间:2015-07-03 05:32:49

标签: objective-c nsmutablearray

我有一个简单的Person类,它定义了一个人的年龄,体重和身高的3个整数。我使用@synthesize为这些变量创建了setter / getter函数,该类包含1个打印出这些变量的printInfo函数。

@import Foundation;

@interface Person : NSObject {
    int age;
    int weight;
    int height;
}
@property int age, weight, height;
-(void) printInfo;
@end

@implementation Person
@synthesize age, weight, height;

-(void) printInfo {
NSLog(@"This person is %i years old, %i feet tall, and weighs %i pounds", age, height, weight);
}
@end

int main(int argc, char *argV[]){
    @autoreleasepool {
        Person *personBuffer = [[Person alloc] init];
        personBuffer.age = 16;
        personBuffer.weight = 135;
        personBuffer.height = 5;

        NSMutableArray *people = [NSMutableArray array];
        [people addObject:personBuffer];

        personBuffer.age = 20;
        personBuffer.weight = 143;
        personBuffer.height = 5;

        [people addObject: personBuffer];

        for(int i = 0; i < [people count]; ++i) {
            [people[i] printInfo];
        }
    }
    return 0;
}

在代码中,我试图创建一个类型为Person的NSMutableArray,并使用Person对象personBuffer向它添加对象,以便我可以在我的程序中动态创建人员。但是,当我运行代码时,不是打印出两个对象的信息,而是打印出我添加两次的第二个对象的信息,我无法弄清楚原因。我写了一些测试:

[people[0] printInfo]
[people[1] printInfo]

这打印出第二个对象两次,所以我知道这是数组本身的问题,而不是打印或打印语句的for循环。我认为它可能是一个指针或内存分配的问题,因为这样的显式内存分配对我来说仍然是一个非常外国的概念来自Python和C ++。

非常感谢帮助搞清楚这一点。先感谢您。

修改:这是我的新代码,但我遇到了更多问题:

@import Foundation;

@interface Person : NSObject {
    int age;
    int weight;
    int height;
}
@property int age, weight, height;
-(void) printInfo;

@end

@implementation Person
@synthesize age, weight, height;

-(void) printInfo {
NSLog(@"This person is %i years old, %i feet tall, and weighs %i pounds", age, height, weight);
}
@end

int main(int argc, char *argV[]){
    @autoreleasepool {
        int inputAge, inputWeight, inputHeight;

        NSMutableArray *people = [NSMutableArray array];
        for(int i = 0; i < 2; ++i) {
            Person *personBuffer = [[Person alloc] init];
            scanf("%s", &inputAge);
            personBuffer.age = inputAge;
            scanf("%s", &inputWeight);
            personBuffer.weight = inputWeight;
            scanf("%s", &inputHeight);
            personBuffer.height = inputHeight;
            [people addObject:personBuffer];
        }


        for(int i = 0; i < [people count]; ++i) {
            [people[i] printInfo];
        }
    }
    return 0;
}

现在,当我输入年龄,身高等值时,程序会输出与我输入的值不同的值。例如,我为年龄值输入了2,程序打印了值50.

提前致谢。

2 个答案:

答案 0 :(得分:0)

对于像

这样的Person对象需要不同的内存分配
    Person *personBuffer1 = [[Person alloc] init];
    personBuffer1.age = 16;
    personBuffer1.weight = 135;
    personBuffer1.height = 5;

    NSMutableArray *people = [NSMutableArray array];
    [people addObject:personBuffer1];

    Person *personBuffer2 = [[Person alloc] init];
    personBuffer2.age = 20;
    personBuffer2.weight = 143;
    personBuffer2.height = 5;

    [people addObject: personBuffer2];

    for(int i = 0; i < [people count]; ++i) {
        [people[i] printInfo];
    }

答案 1 :(得分:0)

您需要添加不同的Person Class对象。 例如。

NSMutableArray *people = [NSMutableArray array];


Person *personBufferOne = [[Person alloc] init];
personBufferOne.age = 16;
personBufferOne.weight = 135;
personBufferOne.height = 5;
[people addObject:personBufferOne];

Person *personBufferTwo = [[Person alloc] init];


personBufferTwo.age = 20;
personBufferTwo.weight = 143;
personBufferTwo.height = 5;

[people addObject: personBufferTwo];

for(int i = 0; i < [people count]; ++i) {
}