Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

ajbrehm

macrumors 6502
Original poster
Aug 14, 2002
341
0
Zurich, Switzerland
I have three class variables:

Code:
NSMutableDictionary *dictEnvironment;
NSMutableArray *listEnvironmentKeys;
NSMutableArray *listEnvironmentValues;

And this code in a method:

Code:
- (void)createEnvironment
{
	NSString *strCurrentEnvironmentKey = @"DISPLAY";
	NSString *strCurrentEnvironmentValue = @"localhost:0";
	[listEnvironmentKeys addObject:strCurrentEnvironmentKey];
	[listEnvironmentValues addObject:strCurrentEnvironmentValue];
	dictEnvironment = [NSDictionary dictionaryWithObjects:listEnvironmentValues forKeys:listEnvironmentKeys];
	[textEnvironmentKey setIntValue:[listEnvironmentKeys count]];
	[textEnvironmentValue setIntValue:[listEnvironmentValues count]];
}

I would expect "[listEnvironmentKeys count]" and "[listEnvironmentValues count]" to be "1" after adding an object to each. But it turns out to be zero.

Also, when I try to get the object from either array, I get an "Invalid parameter not satisfying: aString != nil" error.

Code:
	NSString *strCurrentEnvironmentKey = [listEnvironmentKeys objectAtIndex:countEnvironmentKeys];
	NSString *strCurrentEnvironmentValue = [listEnvironmentValues objectAtIndex:countEnvironmentKeys];
(From another method called later...)

What am I doing wrong?
 

robbieduncan

Moderator emeritus
Jul 24, 2002
25,611
893
Harrogate
You've not posted any code to create Dictionary or Array. You can't add anything to a non-existant object, although Objective-C allows messaging to nil (unlike Java) so no errors get thrown
 

ajbrehm

macrumors 6502
Original poster
Aug 14, 2002
341
0
Zurich, Switzerland
I have changed the method to create the array (declared in the class) rather than add to it:

Code:
- (void)createEnvironment
{
	NSString *strCurrentEnvironmentKey = @"DISPLAY";
	NSString *strCurrentEnvironmentValue = @"localhost:0";
	[listEnvironmentKeys arrayWithObject:strCurrentEnvironmentKey];
	[listEnvironmentValues arrayWithObject:strCurrentEnvironmentValue];
	dictEnvironment = [NSDictionary dictionaryWithObjects:listEnvironmentValues forKeys:listEnvironmentKeys];
	[textEnvironmentKey setIntValue:[listEnvironmentKeys count]];
	[textEnvironmentValue setIntValue:[listEnvironmentValues count]];
}

But now I get "warning: 'NSMutableArray' may not respond to '-arrayWithObject:'" and both counts remain zero still.
 

ajbrehm

macrumors 6502
Original poster
Aug 14, 2002
341
0
Zurich, Switzerland
Does this

Code:
dictEnvironment = [NSDictionary dictionaryWithObjects:listEnvironmentValues forKeys:listEnvironmentKeys];

not create a dictionary?
 

robbieduncan

Moderator emeritus
Jul 24, 2002
25,611
893
Harrogate
Those sort of convenience methods return autoreleased instances. They will disappear at the end of the current run loop. You need to retain them if you want them to stick around. Given the questions and code you have posted I'd say you are a beginner. I'd suggest reading some basic Cocoa tutorials, especially basic memory management as you are fundamentally missing how it all works.

Perhaps start here.

Edit to add: unless there is more code than you have posted there is no "array (declared in the class)". There is simply storage space for a pointer to an array that you have to create.
 

kenkooler

macrumors regular
Jan 2, 2002
195
0
Mexico City
I have changed the method to create the array (declared in the class) rather than add to it:

Code:
- (void)createEnvironment
{
	NSString *strCurrentEnvironmentKey = @"DISPLAY";
	NSString *strCurrentEnvironmentValue = @"localhost:0";
	[listEnvironmentKeys arrayWithObject:strCurrentEnvironmentKey];
	[listEnvironmentValues arrayWithObject:strCurrentEnvironmentValue];
	dictEnvironment = [NSDictionary dictionaryWithObjects:listEnvironmentValues forKeys:listEnvironmentKeys];
	[textEnvironmentKey setIntValue:[listEnvironmentKeys count]];
	[textEnvironmentValue setIntValue:[listEnvironmentValues count]];
}

But now I get "warning: 'NSMutableArray' may not respond to '-arrayWithObject:'" and both counts remain zero still.

I think arrayWithObject: has to be called static from NSMutableArray, as in:

Code:
- (void)createEnvironment
{
	NSString *strCurrentEnvironmentKey = @"DISPLAY";
	NSString *strCurrentEnvironmentValue = @"localhost:0";
	listEnvironmentKeys = [NSMutableArray arrayWithObject:strCurrentEnvironmentKey];
	listEnvironmentValues = [NSMutableArray arrayWithObject:strCurrentEnvironmentValue];
	dictEnvironment = [NSDictionary dictionaryWithObjects:listEnvironmentValues forKeys:listEnvironmentKeys];
	[textEnvironmentKey setIntValue:[listEnvironmentKeys count]];
	[textEnvironmentValue setIntValue:[listEnvironmentValues count]];
}
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.