Re: NSMutableDictionary drives me mad.
Re: NSMutableDictionary drives me mad.
- Subject: Re: NSMutableDictionary drives me mad.
- From: Alexander Heinz <email@hidden>
- Date: Tue, 10 Mar 2009 14:41:23 -0400
Have you tried using NSNumber instead of your custom integer wrapper?
I doubt this is the problem, but see what happens if you do.
HTH,
Alex
On Mar 10, 2009, at 2:33 PM, Тимофей Даньшин wrote:
Hello.
I am writing a method for searching for the longest common
substring. The idea is to store the pieces of that LCS in an
NSMutableDictionary with TMIntWrappers as keys and NSMutableStrings
as values.
Now, TMIntWrapper is the class i created for wrapping ints into
objects. It stores an int, gives access to it via the .value
property, adopts the NSCopying protocol and implements the -(BOOL)
isEqual: (id) object method.
The logic is, if the dictionary already has a value for a given key,
the new value should be appended to the current value, if the
dictionary doesn't have any value for that key yet, a new
NSMutableString is created and stored in that dictionary.
But I just cannot understand why, within one and the same cycle,
some values get appended, and some are stored with seemingly
different TMIntWrapper keys with the same value (even though the
isEqual method returns YES if the int values of two int wrappers are
equal).
Here is the method in question:
- (void) addToAddedCharAtPosition:(int)charPosition withKey:
(TMIntWrapper *)key {
NSLog
(@"___________________________________________________________________________________________
");
NSLog (@" The key is: %@", key);
NSLog (@" The object for the key is: %@", [addedToDbString
objectForKey:key]);
if ([addedToDbString objectForKey:key] == nil) {
NSMutableString *ms = [[NSMutableString alloc]initWithString:
[inText substringWithRange:NSMakeRange(charPosition-1, 1)]];
NSLog(@" The ms string is: %@", ms);
[addedToDbString setObject:[ms mutableCopy] forKey:[key copy]];
NSLog(@" We are trying to add this: %@", [inText
substringWithRange:NSMakeRange(charPosition-1, 1)]);
NSLog (@"AND NOW: The object for the key is: %@", [addedToDbString
objectForKey:key]);
return;
}
NSLog (@"We are trying to add this: %@",[inText
substringWithRange:NSMakeRange(charPosition-1, 1)]);
[[addedToDbString objectForKey:key] appendString:[inText
substringWithRange:NSMakeRange(charPosition-1, 1)]];
}
And here is what those "NSLog"s generate.
2009-03-10 21:09:48.871 SQL doc[13532:10b]
___________________________________________________________________________________________
2009-03-10 21:09:48.872 SQL doc[13532:10b]
The key is: 0
2009-03-10 21:09:48.872 SQL doc[13532:10b] The object for
the key is: (null)
2009-03-10 21:09:48.873 SQL doc[13532:10b] The ms
string is: x
2009-03-10 21:09:48.874 SQL doc[13532:10b] We are trying to
add this: x
2009-03-10 21:09:48.875 SQL doc[13532:10b] AND NOW: The object for
the key is: (null)
2009-03-10 21:09:48.875 SQL doc[13532:10b]
___________________________________________________________________________________________
2009-03-10 21:09:48.877 SQL doc[13532:10b]
The key is: 0
2009-03-10 21:09:48.877 SQL doc[13532:10b] The object for
the key is: (null)
2009-03-10 21:09:48.879 SQL doc[13532:10b] The ms
string is: e
2009-03-10 21:09:48.880 SQL doc[13532:10b] We are trying to
add this: e
2009-03-10 21:09:48.880 SQL doc[13532:10b] AND NOW: The object for
the key is: e
2009-03-10 21:09:48.880 SQL doc[13532:10b]
___________________________________________________________________________________________
Please help me...
_______________________________________________
Cocoa-dev mailing list (email@hidden)
Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden