I am trying to make multiple instances of a class, but I've figured out that they are acting like one instance. or possibly there is a loop that I'm not seeing.
mainClass.m
Code:
#import "instanceTestViewController.h"
#import "otherClass.h"
NSMutableArray *OptionSet;
- (void)viewDidLoad
{
[super viewDidLoad];
OptionSet = [[NSMutableArray alloc] init];
[self makeInstances];
[self setInstanceColors];
[self showColors];
}
-(void)makeInstances
{
otherClass *Temp = [[otherClass alloc] init];
for( int i = 0; i < 6; i++ )
{
[OptionSet addObject:Temp];
}
[Temp release];
}
-(void)setInstanceColors
{
// should call the load method of each instance, and set each "myColor"
for (int i = 0; i < 6; i++){
temp = [OptionSet objectAtIndex:i];
[temp load];
[temp set_myColor:[UIColor yellowColor]];
}
temp = [OptionSet objectAtIndex:0];
/* If the next line is quoted out, then "showColors" changes all the boxes'
background color to yellow. But if it is left in, all boxes show as blue. */
[temp set_myColor:[UIColor blueColor]];
[temp release];
}
-(IBAction)showColors
{
otherClass *temp = [[otherClass alloc] init];
temp = [OptionSet objectAtIndex:0];
[box1 setBackgroundColor:[temp get_theColor]];
temp = [OptionSet objectAtIndex:1];
[box2 setBackgroundColor:[temp get_theColor]];
temp = [OptionSet objectAtIndex:2];
[box3 setBackgroundColor:[temp get_theColor]];
[temp release];
}
I am trying to make multiple instances of a class, but I've figured out that they are acting like one instance. or possibly there is a loop that I'm not seeing.
mainClass.m
Code:
#import "instanceTestViewController.h"
#import "otherClass.h"
NSMutableArray *OptionSet;
- (void)viewDidLoad
{
[super viewDidLoad];
OptionSet = [[NSMutableArray alloc] init];
[self makeInstances];
[self setInstanceColors];
[self showColors];
}
-(void)makeInstances
{
otherClass *Temp = [[otherClass alloc] init];
for( int i = 0; i < 6; i++ )
{
[OptionSet addObject:Temp];
}
[Temp release];
}
-(void)setInstanceColors
{
// should call the load method of each instance, and set each "myColor"
for (int i = 0; i < 6; i++){
temp = [OptionSet objectAtIndex:i];
[temp load];
[temp set_myColor:[UIColor yellowColor]];
}
temp = [OptionSet objectAtIndex:0];
/* If the next line is quoted out, then "showColors" changes all the boxes'
background color to yellow. But if it is left in, all boxes show as blue. */
[temp set_myColor:[UIColor blueColor]];
[temp release];
}
-(IBAction)showColors
{
otherClass *temp = [[otherClass alloc] init];
temp = [OptionSet objectAtIndex:0];
[box1 setBackgroundColor:[temp get_theColor]];
temp = [OptionSet objectAtIndex:1];
[box2 setBackgroundColor:[temp get_theColor]];
temp = [OptionSet objectAtIndex:2];
[box3 setBackgroundColor:[temp get_theColor]];
[temp release];
}
You do realize that is the exactly code he has written....WTF.
Do this:
Code:
for( int i = 0; i < 6; i++ )
{
otherClass *Temp = [[otherClass alloc] init];
[OptionSet addObject:Temp];
[Temp release];
}
Actually i referrred to his code and told him what was the mistake, just read my full sentence, in the second line i gave the solution. If you have some problem then you may suggest to others but i don't think there is any need to use words like 'WTF'.
__________________ iOS Developerz
You Think, We Create...
Thank you both for pointing out that flaw. My code is now
Code:
for( int i = 0; i < 6; i++ )
{
otherClass *Temp = [[otherClass alloc] init];
[OptionSet addObject:Temp];
[Temp release];
}
otherClass *temp = [[otherClass alloc] init];
for (int i = 0; i < 6; i++){
temp = [OptionSet objectAtIndex:i];
[temp viewDidLoad];
[temp set_myColor:[UIColor yellowColor]];
}
[temp release];
otherClass *help = [[otherClass alloc] init];
help = [OptionSet objectAtIndex:0];
[help set_myColor:[UIColor blueColor]];
[help release];
this still produces the same results. so far, when ever i call the "set_myColor", it changes the color of all the instances of otherClass.
I have also tried making the instances manually (without a loop), which yielded the same results. I also called set_myColor individually instead of a loop, which also had the same results.
Could it still be that they are technically the same instance? could it be an issue with how I have set up the "otherClass"? if it helps, I haven't added anything to otherClass.h. Should i place my methods and variables in the .h?
ok. The fix to this is to set temp equal to an object in the array. call the methods or functions of temp. Then to replace the object currently in the array with the new temp.
NEW CODE
Code:
-(void)viewDidLoad{
[super viewDidLoad];
for (int i = 0; i < 3; i++)
{
otherClass *Temp = [[otherClass alloc] init];
[Temp load];
[Temp set_myColor:[UIColor blueColor]];
[OptionSet addObject:Temp];
[Temp release];
}
}
-(IBAction)showColors
{
otherClass *temp = [[otherClass alloc] init]; // make instance
temp = [OptionSet objectAtIndex:1]; // set equal to object in array
[temp set_myColor:[UIColor greenColor]]; //call function in temp
[OptionSet replaceObjectAtIndex:1 withObject:temp]; // replace
temp = [OptionSet objectAtIndex:0]; //set again to see if it worked
box1.backgroundColor = [temp get_boxColor]; //makes the box blue
[temp release];
}
ok. The fix to this is to set temp equal to an object in the array. call the methods or functions of temp. Then to replace the object currently in the array with the new temp.
NEW CODE
Code:
-(void)viewDidLoad{
[super viewDidLoad];
for (int i = 0; i < 3; i++)
{
otherClass *Temp = [[otherClass alloc] init];
[Temp load];
[Temp set_myColor:[UIColor blueColor]];
[OptionSet addObject:Temp];
[Temp release];
}
}
-(IBAction)showColors
{
otherClass *temp = [[otherClass alloc] init]; // make instance
temp = [OptionSet objectAtIndex:1]; // set equal to object in array
[temp set_myColor:[UIColor greenColor]]; //call function in temp
[OptionSet replaceObjectAtIndex:1 withObject:temp]; // replace
temp = [OptionSet objectAtIndex:0]; //set again to see if it worked
box1.backgroundColor = [temp get_boxColor]; //makes the box blue
[temp release];
}
Thanks again to both of you for your insight
This is most likely the incorrect way to do this. Your allocating an instance, setting that instance to an array object, and then releasing the array object?
__________________
Haters gonna Hate
Likers gonna Like