Advertise here




Advertise here

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Sign In with Google Sign In with OpenID
Please do not post the same thing multiple times. The board software automatically flags certain posts as needing moderator attention. This happens the most often for new users. I'm pretty sure this is made clear at the time you attempt to post. Posting the same thing over and over again just makes that many more posts the moderators have to weed through later. This makes us sad. Don't make us sad. If your post/thread doesn't appear, just wait a while. Don't post it again. If it hasn't shown up by the next day, then you can try again. I normally go through posts in the mornings, and try to check a few times throughout the day, but I'm not here 24/7. There will typically be a significant delay before posts are approved. Just be patient.

What is wrong with my class instances?

twhitetwhite Posts: 3New Users
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

#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];
}



otherClass.m
#import \"Color.h\"

@implementation Color

UIColor *myColor;

-(void)load
{
myColor = [[UIColor alloc] init];
}

-(void)set_myColor:(UIColor*)newcolor
{
myColor = newcolor;
}

-(UIColor*)get_theColor
{
return myColor;
}



mainClass.h
#import <UIKit/UIKit.h>

@interface CrackTheCodeHelperViewController : UIViewController {
IBOutlet UITextField *box1;
IBOutlet UITextField *box2;
IBOutlet UITextField *box3;
}
@property (nonatomic, retain) UITextField *box1;
@property (nonatomic, retain) UITextField *box2;
@property (nonatomic, retain) UITextField *box3;

// each of the boxes is attached appropriately in my .xib file

@end
Post edited by twhite on

Replies

  • iOSDeveloperziOSDeveloperz Posts: 99Registered Users
    twhite;340120 said:
    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

    #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];
    }



    otherClass.m
    #import \"Color.h\"

    @implementation Color

    UIColor *myColor;

    -(void)load
    {
    myColor = [[UIColor alloc] init];
    }

    -(void)set_myColor:(UIColor*)newcolor
    {
    myColor = newcolor;
    }

    -(UIColor*)get_theColor
    {
    return myColor;
    }



    mainClass.h
    #import <UIKit/UIKit.h>

    @interface CrackTheCodeHelperViewController : UIViewController {
    IBOutlet UITextField *box1;
    IBOutlet UITextField *box2;
    IBOutlet UITextField *box3;
    }
    @property (nonatomic, retain) UITextField *box1;
    @property (nonatomic, retain) UITextField *box2;
    @property (nonatomic, retain) UITextField *box3;

    // each of the boxes is attached appropriately in my .xib file

    @end
     otherClass *Temp = [[otherClass alloc] init];
    for( int i = 0; i < 6; i++ )
    {
    [OptionSet addObject:Temp];
    }
    adds the same object which is "Temp" to all the positions of an array.
    Perform looping in "viewDidLoad" where you call "[self makeInstances]"
    iOS Developerz

    You Think, We Create...



    http://iosdeveloperz.com/



    <a hre
  • Bertrand21Bertrand21 Posts: 2,009Registered Users
    iOSDeveloperz;340128 said:
     otherClass *Temp = [[otherClass alloc] init];
    for( int i = 0; i < 6; i++ )
    {
    [OptionSet addObject:Temp];
    }
    adds the same object which is "Temp" to all the positions of an array.
    Perform looping in "viewDidLoad" where you call "[self makeInstances]"
    You do realize that is the exactly code he has written....WTF.

    Do this:
     
    for( int i = 0; i < 6; i++ )
    {
    otherClass *Temp = [[otherClass alloc] init];
    [OptionSet addObject:Temp];
    [Temp release];
    }
    Acquired.
  • iOSDeveloperziOSDeveloperz Posts: 99Registered Users
    Bertrand21;340129 said:
    You do realize that is the exactly code he has written....WTF.

    Do this:
     
    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...



    http://iosdeveloperz.com/



    <a hre
  • twhitetwhite Posts: 3New Users
    Thank you both for pointing out that flaw. My code is now

    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?
  • twhitetwhite Posts: 3New Users
    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

    -(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 :D
  • Bertrand21Bertrand21 Posts: 2,009Registered Users
    twhite;340440 said:
    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

    -(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 :D
    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?
    Acquired.
Sign In or Register to comment.