Re: Get my custom object from NSDictionary variable
Re: Get my custom object from NSDictionary variable
- Subject: Re: Get my custom object from NSDictionary variable
- From: Graham Cox <email@hidden>
- Date: Fri, 5 Mar 2010 11:00:11 +1100
On 05/03/2010, at 10:48 AM, Daniel Káčer wrote:
> a add my custom object into NSDictionary variable in my application with following code:
>
> [myDictionary setObject:[[ComplexObject alloc] initWithFrom:_tempFrom pairTo:string] forKey:[NSString stringWithFormat:@"%d", [myDictionary count]]];
>
> .. it seems, that this work correctly ..
Except it's leaking. Don't put the alloc...init inside another statement. For one thing, if it fails you'll throw instead of continue. But more importantly, you don't have a reference to the object to release it once the dictionary has taken ownership, so it leaks.
> but .. when i will retrieve value from my custom object from this NSDictionary variable, there is some issue ... and i don't know, what i do wrongly in this case ...
>
> int iRandom = arc4random() % ([myDictionary count] - 1);
> ComplexObject* compObj = [myDictionary objectForKey:[NSString stringWithFormat:@"%d", iRandom]];
> NSString* sText = [compObj valueFrom]; <--- on this line is some issue
Well, what is the issue?
> - (ComplexObject*)initWithFrom:(NSString*)_sValueFrom pairTo:(NSString*)_sValueTo {
> self = [super init];
>
> if (self) {
> sValueFrom = _sValueFrom;
> sValueTo = _sValueTo;
> }
> return self;
> }
Simply assigning the strings to the ivars is inadequate. You also need to retain them, so I'm guessing the 'issue' is that you get an EXC_BAD_ACCESS because the returned references are stale.
You need to read this. Take three times a day every four hours.
http://developer.apple.com/mac/library/documentation/Cocoa/Conceptual/MemoryMgmt/Articles/mmRules.html
Also, your naming conventions and use of underscores is all over the place.
--Graham
_______________________________________________
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