Advertise Mobile SDKs Books Events Forum News Social Networking Support Us
Follow @iphonedevsdk on Twitter

Interface 2, Advanced iOS
Mockup & Code Gen
($9.99)

Make your own iPhone apps
and run them live!
(free)

Pic Frame Dynamo: Photo Editing
($0.99)

Abiliator
($1.99)

Want your application or service advertised on iPhone Dev SDK?

Go Back   iPhone Dev SDK Forum > iPhone SDK Development Forums > iPhone SDK Development

Reply
 
LinkBack Thread Tools Display Modes
Old 05-31-2011, 12:56 AM   #1 (permalink)
Registered Member
 
Join Date: May 2011
Posts: 3
twhite is on a distinguished road
Question What is wrong with my class instances?

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];
}
otherClass.m
Code:
#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
Code:
#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
twhite is offline   Reply With Quote
Old 05-31-2011, 01:18 AM   #2 (permalink)
iOSDeveloperz
 
iOSDeveloperz's Avatar
 
Join Date: Apr 2011
Location: INDIA
Posts: 99
iOSDeveloperz is on a distinguished road
Send a message via Skype™ to iOSDeveloperz
Default

Quote:
Originally Posted by twhite View Post
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];
}
otherClass.m
Code:
#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
Code:
#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
Code:
 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/

View our latest apps
iOSDeveloperz is offline   Reply With Quote
Old 05-31-2011, 01:22 AM   #3 (permalink)
Beast Mode
 
Join Date: Dec 2008
Age: 21
Posts: 1,971
Bertrand21 is on a distinguished road
Default

Quote:
Originally Posted by iOSDeveloperz View Post
Code:
 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:
Code:
 
	for( int i = 0; i < 6; i++ )
	{
                otherClass *Temp = [[otherClass alloc] init];
		[OptionSet addObject:Temp];
                [Temp release];
	}
__________________
Haters gonna Hate
Likers gonna Like
Bertrand21 is offline   Reply With Quote
Old 05-31-2011, 05:03 AM   #4 (permalink)
iOSDeveloperz
 
iOSDeveloperz's Avatar
 
Join Date: Apr 2011
Location: INDIA
Posts: 99
iOSDeveloperz is on a distinguished road
Send a message via Skype™ to iOSDeveloperz
Default

Quote:
Originally Posted by Bertrand21 View Post
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...

http://iosdeveloperz.com/

View our latest apps

Last edited by iOSDeveloperz; 05-31-2011 at 05:11 AM.
iOSDeveloperz is offline   Reply With Quote
Old 05-31-2011, 06:10 PM   #5 (permalink)
Registered Member
 
Join Date: May 2011
Posts: 3
twhite is on a distinguished road
Default

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?

Last edited by twhite; 05-31-2011 at 06:12 PM.
twhite is offline   Reply With Quote
Old 05-31-2011, 07:36 PM   #6 (permalink)
Registered Member
 
Join Date: May 2011
Posts: 3
twhite is on a distinguished road
Default GOT IT!

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

Last edited by twhite; 05-31-2011 at 07:46 PM.
twhite is offline   Reply With Quote
Old 05-31-2011, 11:08 PM   #7 (permalink)
Beast Mode
 
Join Date: Dec 2008
Age: 21
Posts: 1,971
Bertrand21 is on a distinguished road
Default

Quote:
Originally Posted by twhite View Post
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
Bertrand21 is offline   Reply With Quote
Reply

Bookmarks

Tags
class, code, instance, nsmutablearray, problems

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On



» Advertisements
» Online Users: 336
6 members and 330 guests
Dnnake, dre, iOS.Lover, jenniead38, Kirkout, Wikiboo
Most users ever online was 1,387, 04-10-2012 at 04:21 AM.
» Stats
Members: 175,663
Threads: 94,120
Posts: 402,898
Top Poster: BrianSlick (7,990)
Welcome to our newest member, LezB44
Powered by vBadvanced CMPS v3.1.0

All times are GMT -5. The time now is 02:16 AM.
Powered by vBulletin® Version 3.8.0
Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.3.0