PDA

View Full Version : NSArray/NSMutableArray problem




ajbrehm
Apr 9, 2007, 01:13 PM
I have three class variables:


NSMutableDictionary *dictEnvironment;
NSMutableArray *listEnvironmentKeys;
NSMutableArray *listEnvironmentValues;


And this code in a method:


- (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.


NSString *strCurrentEnvironmentKey = [listEnvironmentKeys objectAtIndex:countEnvironmentKeys];
NSString *strCurrentEnvironmentValue = [listEnvironmentValues objectAtIndex:countEnvironmentKeys];

(From another method called later...)

What am I doing wrong?



robbieduncan
Apr 9, 2007, 01:23 PM
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
Apr 9, 2007, 04:06 PM
I have changed the method to create the array (declared in the class) rather than add to it:


- (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
Apr 9, 2007, 04:08 PM
Does this


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


not create a dictionary?

robbieduncan
Apr 9, 2007, 04:28 PM
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 (http://www.cocoadev.com/index.pl?MemoryManagement).

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.

GeeYouEye
Apr 9, 2007, 04:50 PM
Er... are you sure they're class variables? I thought those weren't allowed in Objective-C anymore? Do you mean instance variables?

robbieduncan
Apr 9, 2007, 05:08 PM
Er... are you sure they're class variables? I thought those weren't allowed in Objective-C anymore? Do you mean instance variables?

You can have class variables by declaring them static.

kenkooler
Apr 9, 2007, 10:59 PM
I have changed the method to create the array (declared in the class) rather than add to it:


- (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:


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