Friday, 15 April 2011

objective c - How to stop addObject from overwriting all previous objects in a NSMutableArray -



objective c - How to stop addObject from overwriting all previous objects in a NSMutableArray -

addobject overwriting previous cells. if array size 4, adding objects increases size, overwrites other cells same thing.

groups = [[nsmutablearray alloc]init]; [groups addobject:[[group alloc] init]]; [[groups objectatindex:0] setgroupname:@"cs480"]; [[groups objectatindex:0] setpassword:@"apple1"]; [groups addobject:[[group alloc] init]]; [[groups objectatindex:1] setgroupname:@"cs481"]; [[groups objectatindex:1] setpassword:@"apple2"]; +(void) print{ nslog(@"size: %lu",(unsigned long)[groups count]); for(int = 0; < [groups count]; i++){ grouping *group = [groups objectatindex:i]; nslog(@"%@\t%@\n",[group getgroupname], [group getpassword]); } }

the grouping class is

#import <foundation/foundation.h> #import "group.h" #import "person.h" #import "pie.h" @implementation grouping nsstring* groupname; nsstring* password; nsmutablearray *groupmembers; nsmutablearray *skillsofmembers; -(id) init{ self = [super init]; if(self){ nslog(@"inside grouping constructor"); groupmembers = [[nsmutablearray alloc] init]; skillsofmembers = [[nsmutablearray alloc] init]; groupname = [[nsstring alloc]init]; password = [[nsstring alloc]init]; } homecoming self; } -(nsinteger) authenticate:(nsstring *)checkgroupname :(nsstring *)checkpassword{ if([checkgroupname isequaltostring:groupname]&&[checkpassword isequaltostring:password]){ homecoming 1; } homecoming 0; } -(void) setgroup:(nsmutablearray*)newgroupmembers{ groupmembers = newgroupmembers; } -(nsmutablearray*) getgroup{ homecoming groupmembers; } -(person*) getperson:(int) index{ homecoming [groupmembers objectatindex:index]; } -(void) setperson:(int) index :(person*) newperson{ person* person = [groupmembers objectatindex:index]; person = newperson; } -(nsmutablearray*) getskillsofperson:(int)index{ homecoming [skillsofmembers objectatindex:index]; } -(void) setskillsofperson:(pie*) newskills :(int)index{ pie *skillsofmember = [skillsofmembers objectatindex:index]; skillsofmember = newskills; } -(void) setgroupname:(nsstring*) newname{ groupname = [newname copy]; } -(nsstring*) getgroupname{ homecoming groupname; } -(void) setpassword:(nsstring*) newpassword{ password = [newpassword copy]; } -(nsstring*) getpassword{ homecoming password; } @end

the output

2014-11-10 13:23:19.013 balanced team pie[1964:66289] size: 2 2014-11-10 13:23:19.013 balanced team pie[1964:66289] cs481 apple2 2014-11-10 13:23:19.014 balanced team pie[1964:66289] cs481 apple2

what issue?

these:

@implementation grouping nsstring* groupname; nsstring* password; nsmutablearray *groupmembers; nsmutablearray *skillsofmembers;

are not instance variables. global variables. declared @ file scope. fact come between @implementation , corresponding @end means nil compiler.

effectively, there's 1 set of variables whole program, rather 1 set of variables each instance of group class.

you meant:

@implementation grouping { nsstring* groupname; nsstring* password; nsmutablearray *groupmembers; nsmutablearray *skillsofmembers; }

the curly braces required create variables instance variables.

objective-c xcode

No comments:

Post a Comment