简体   繁体   English

将自定义对象添加到NSMutableArray

[英]Adding Custom Objects to NSMutableArray

I mainly program in Java and can't understand why this isn't working. 我主要使用Java编程,无法理解为什么它不起作用。 I'm trying to create a temporary object "Judge" in my for loop. 我正在尝试在我的for循环中创建一个临时对象“ Judge”。 I then want to add that object to an NSMutableArray so in the end I have an array filled with different Judge objects. 然后,我想将该对象添加到NSMutableArray中,以便最终得到一个填充有不同Judge对象的数组。 After the for loop I run through all the objects in the Array and they're all the last Judge Object. 在for循环之后,我遍历了数组中的所有对象,它们都是最后一个判断对象。

The NSLog shows that "JudgeTemp" object is being assigned the right values while in the for loop. NSLog显示在for循环中为“ JudgeTemp”对象分配了正确的值。 My guess is that it's not creating a new object called JudgeTemp every time but referencing the old already created JudgeTemp. 我的猜测是,它并不是每次都创建一个名为JudgeTemp的新对象,而是引用已经创建的旧JudgeTemp。

NSMutableArray *Judges = [NSMutableArray arrayWithCapacity:30];

for (int i=0; i<[courtinfoarray count]; i++) {
    Judge1= [[courtinfoarray objectAtIndex:i] componentsSeparatedByString:@"|"];

     Judge *JudgeTemp=[[Judge alloc]init];

    [JudgeTemp setName:[Judge1 objectAtIndex:0] picture:[Judge1 objectAtIndex:1] courtroom:[Judge1 objectAtIndex:2] phone:[Judge1 objectAtIndex:3] undergrad:[Judge1 objectAtIndex:4] lawschool:[Judge1 objectAtIndex:5] opdasa:[Judge1 objectAtIndex:6] career:[Judge1 objectAtIndex:7] judgecode:[Judge1 objectAtIndex:8]];

    NSLog(@"%@",[JudgeTemp getName]);
    [Judges addObject:JudgeTemp];
    NSLog(@"%@",[[Judges objectAtIndex:i]getName]);

}

Judges Class 法官班

@implementation Judge

NSString *name;
NSString *picture;
NSString *courtroom;
NSString *phone;
NSString *undergrad;
NSString *lawschool;
NSString *opdasa;
NSString *career;
NSString *judgecommentcode;


-(void) setName:(NSString *)n picture:(NSString *) p courtroom:(NSString *)c phone:(NSString *)ph undergrad: (NSString *) u lawschool: (NSString *)l opdasa: (NSString *) o career: (NSString *)ca judgecode: (NSString *)jcode{

name = n;
picture = p;
courtroom = c;
phone = ph;
undergrad = u;
lawschool = l;
opdasa = o;
career = ca;
judgecommentcode = jcode;

}
-(NSString*) getName{

return name;
}

The problem is with your Judge class. 问题出在您的Judge班上。 When you define variables directly in your @implementation they have global scope and are not instance variables. 当直接在@implementation定义变量时,它们具有全局作用域,而不是实例变量。 What you need to do is put those variable declarations in your @interface instead: 您需要做的是将这些变量声明放在@interface

@interface Judge : NSObject {
    NSString *name;
    NSString *picture;
    NSString *courtroom;
    NSString *phone;
    NSString *undergrad;
    NSString *lawschool;
    NSString *opdasa;
    NSString *career;
    NSString *judgecommentcode;
}

// ...

@end

Edit: Apparently you can declare them in your @implementation , you just have to wrap them in { } . 编辑:显然,您可以@implementation声明它们,只需要将它们包装在{ } See: Instance variables declared in ObjC implementation file 请参阅: ObjC实现文件中声明的实例变量

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM