[英]trouble adding multiple objects to an 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。
提前致謝。
它需要為 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];
}
您需要添加不同的 Person 類對象。 例如。
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) {
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.