objective c - Object added to Array is immediately 'out of scope' -


i'm new objective c , use assistance.

i have created class called agent. agent class contains following method:

    + (agent *)agentwithname:(nsstring*)thename {          agent *agent = [[[agent alloc] init] autorelease];          agent.agentname = thename;         return agent;     } 

then root view controller want loop through dictionary of names creating agent object each name , adding agent object nsmutablearray:

    (id object in dictarray) {         nsstring *agentname = [object objectforkey:@"name"];         [self.myagents addobject:[agent agentwithname:agentname]];     } 

the trouble execution has passed [self.myagents addobject:[agent agentwithname:agentname]];, agent objects inside of nsmutablearray self.myagents listed debugger 'out of scope'. causes exc_bad_access later in code when try access objects in array. objects getting added array (at least they're showing in xcode debugger) they're out of scope, they're out of scope before exiting loop. can please explain i'm doing wrong? i'm has lack of understanding memory management. taking look.

i have created simple nsobject class agent:

//  agent.h  @interface agent : nsobject  @property (nonatomic, copy) nsstring *agentname;  - (id)initwithagentname:(nsstring *)name;  @end  // agent.m  @implementation agent  - (id)initwithagentname:(nsstring *)name {     self = [super init];     if (self) {         // custom initialization          self.agentname = name;     }     return self; } 

and create instances like:

// assuming dictarray contains nsdictionaries code implies (id dictionary in dictarray) {     nsstring *agentname = [dictionary objectforkey:@"name"];     agent *agent = [[agent alloc] initwithagentname:agentname];     [self.myagents addobject:agent]; } 

Comments

Popular posts from this blog

objective c - Change font of selected text in UITextView -

php - Accessing POST data in Facebook cavas app -

c# - Getting control value when switching a view as part of a multiview -