0

I mainly program in Java and can't understand why this isn't working. I'm trying to create a temporary object "Judge" in my for loop. I then want to add that object to an NSMutableArray so in the end I have an array filled with different Judge objects. After the for loop I run through all the objects in the Array and they're all the last Judge Object.

The NSLog shows that "JudgeTemp" object is being assigned the right values while in the for loop. My guess is that it's not creating a new object called JudgeTemp every time but referencing the old already created 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;
}
  • You're creating all the objects with the same data, so how do you know they're the same object? It looks like you should be getting different objects, but if you've made Judge a singleton or otherwise monkeyed with the object creation process then you might indeed get the same object every time. – Caleb Aug 31 '14 at 15:41
  • They don't all have the same data (he/she logs the results at each iteration and said they don't) and there does not appear to be a singleton or anything like that here. – Dima Aug 31 '14 at 15:45
  • I run through the Judges Array after the for loop and every object is the last object created in the for loop. Other answers seem to point to adding [JudgeTemp release] but I can't do that in ARC. I should also mention the "Judge1" variable is poorly named but it is an Array of NSStrings. – MatthewExpungement Aug 31 '14 at 15:45
  • 1
    Please show the code for your `Judge` class. Since all your `Judge` objects contain the values of the last assignment, I'm guessing you're using class variables instead of instance variables for `name`, `picture`, etc. – Rob Bajorek Aug 31 '14 at 16:03
  • What do you see if you simply log the `Judges` array after the loop rather than individual elements? (`NSLog(@"Full array: %@", Judges);`) – Phillip Mills Aug 31 '14 at 16:19
  • 2014-08-31 12:23:23.943 NotGuiltyApp[1789:60b] Full Array: ( "", "", "", "", "", "", "", "", "", "", "" ) – MatthewExpungement Aug 31 '14 at 16:24

1 Answers1

3

The problem is with your Judge class. When you define variables directly in your @implementation they have global scope and are not instance variables. What you need to do is put those variable declarations in your @interface instead:

@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 { }. See: Instance variables declared in ObjC implementation file

Community
  • 1
  • 1
Mike S
  • 41,895
  • 11
  • 89
  • 84