如何阻止addObject覆盖NSMutableArray中的所有先前对象

时间:2014-11-10 21:23:55

标签: objective-c xcode

addObject会覆盖所有以前的单元格。因此,如果数组大小为4,则添加对象会增加大小,但它也会用相同的内容覆盖所有其他单元格。

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 i = 0; i < [groups count]; i++){
        Group *group = [groups objectAtIndex:i];
        NSLog(@"%@\t%@\n",[group getGroupName], [group getPassword]);

    }

}

小组课程是

#import <Foundation/Foundation.h>

#import "Group.h"
#import "Person.h"
#import "Pie.h"

@implementation Group
NSString* groupName;
NSString* password;
NSMutableArray *groupMembers;
NSMutableArray *skillsOfMembers;
-(id) init{
    self = [super init];
    if(self){
         NSLog(@"inside group constructor");
         groupMembers = [[NSMutableArray alloc] init];
         skillsOfMembers = [[NSMutableArray alloc] init];
         groupName = [[NSString alloc]init];
         password =  [[NSString alloc]init];
     }
     return self;
 }

 -(NSInteger) authenticate:(NSString *)checkGroupName :(NSString *)checkPassword{
    if([checkGroupName isEqualToString:groupName]&&[checkPassword isEqualToString:password]){
        return 1;
    }
    return 0;
}

-(void) setGroup:(NSMutableArray*)newGroupMembers{
    groupMembers = newGroupMembers;
}
-(NSMutableArray*) getGroup{
    return groupMembers;
}
-(Person*) getPerson:(int) index{
    return [groupMembers objectAtIndex:index];
}
-(void) setPerson:(int) index :(Person*) newPerson{
    Person* person = [groupMembers objectAtIndex:index];
    person = newPerson;
}


-(NSMutableArray*) getSkillsOfPerson:(int)index{
    return [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{
    return groupName;
}

-(void) setPassword:(NSString*) newPassword{
     password = [newPassword copy];
}

-(NSString*) getPassword{
     return password;
}
@end

输出

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

问题是什么?

1 个答案:

答案 0 :(得分:0)

这些:

@implementation Group
NSString* groupName;
NSString* password;
NSMutableArray *groupMembers;
NSMutableArray *skillsOfMembers;

不是实例变量。它们只是全局变量。它们在文件范围内声明。它们介于@implementation及其对应的@end之间的事实对编译器没有任何意义。

实际上,整个程序有一组变量,而不是Group类的每个实例的一组变量。

你可能意味着:

@implementation Group
{
    NSString* groupName;
    NSString* password;
    NSMutableArray *groupMembers;
    NSMutableArray *skillsOfMembers;
}

花括号需要使变量实例变量。