PDA

View Full Version : NSMutableArray + Retrival




semaja2
Jan 31, 2007, 09:31 PM
Hi guys,

I am working on my first OBJ-C project but i have stumbled across a problem when trying to retrive the actual text thats being stored in a NSMutableArray

What i need to be able to do is find with in the "SSID" object the name of the SSID i have in a variable, then with that found i need to be able to grab the connectSourceScript that it has in another object...

this what i have so far, but it wont work really at all due to i cant work out how to match the SSIDs

/*works*/
script = [[ssidDic objectAtIndex:0] objectForKey:@"connectSource"];
/*only partly works result = ("display dialog hello", "--disconnect ScriptCode") */
script = [ssidDic valueForKey:@"disconnectSource"];



i hope this makes senses



kainjow
Jan 31, 2007, 10:09 PM
Arrays in Cocoa hold objects. If it holds NSString objects, then you go through each item in the array and check to see if the string is equal to a string variable you have somewhere.

If you're just trying to modify a project, post some more code. If you're trying to build something from scratch, I'd suggest reading up more on Cocoa. This is basic stuff that is probably covered in the beginning of any Cocoa book.

HiRez
Feb 1, 2007, 01:47 AM
Just a general tip, if you're comparing strings for equality of content, don't use if aString == anotherString, use if ([aString isEqualToString:anotherString]).

caveman_uk
Feb 1, 2007, 02:33 AM
Just a general tip, if you're comparing strings for equality of content, don't use if aString == anotherString, use if ([aString isEqualToString:anotherString]).
Because the first one compares the object memory address i.e are the two variables pointing at the exact same object. The second compares the contents of the objects i.e. are the characters the same.

whooleytoo
Feb 1, 2007, 07:08 AM
/*works*/
script = [[ssidDic objectAtIndex:0] objectForKey:@"connectSource"];
/*only partly works result = ("display dialog hello", "--disconnect ScriptCode") */
script = [ssidDic valueForKey:@"disconnectSource"];





There isn't enough in this snippet to see how you're storing your data, but it looks like you're trying to store name, value pairs.

If so, you might consider using NSMutableDictionary instead of NSMutableArray.

semaja2
Feb 1, 2007, 08:52 AM
I know i should of uploaded the full code but i have worked out how to do it, heres the code for anyone else that might find this later.

NSEnumerator *searchEnum = [ssidDic objectEnumerator];
BOOL ssidFound = NO;
NSMutableDictionary *nextSsidDict;
while (nextSsidDict = [searchEnum nextObject]) {
if ([[nextSsidDict objectForKey:@"SSID"] isEqualToString:ssid]) {
ssidFound = YES;
source = [nextSsidDict objectForKey:script];
break;
}
}