我有两个包含工作人员的阵列。一个数组包含酒店的所有工作人员作为Staff
个对象,另一个数组仅包含当前作为Staff
个对象工作的工作人员。
我有一个自定义对象;
Staff.h
NSString * staff_id;
NSString * staff_name;
BOOL isWorking
我的最后一个数组是包含所有工作人员,每个工作人员只有一个实例(所以每个工作人员只在数组中出现一次)和Staff
个对象的BOOL值设置为{{1}工作人员在哪里工作。默认情况下,NO/FALSE/0
设置为TRUE。
我尝试了不同条件语句的组合,但我仍然得到重复。我的条件陈述绝对不是最佳的。我试图最大限度地开始使用更加优化的解决方案。
isWorking
我一直在寻求改进,所以欢迎任何有用的想法和建设性的批评。
目前的结果是-(void)sortStaff{
NSMutableArray * fullArray = [NSMutableArray array];//contain full array of staff objects
NSMutableArray * idArray = [NSMutableArray array];//contain all staff ids in order to check whether they already exist in the array or not
NSMutableArray * noDuplicatesArray = [NSMutableArray array];//the array to hold no duplicates
//loop through all staff working
for (int i =1; i<self.workingStaffArray.count; i++) {
Staff * workingStaffMember = [[Staff alloc] init];
workingStaffMember = [self.workingStaffArray objectAtIndex:i];
//loop through all the staff of the hotel
for (int j=0; j<self.allStaffArray.count; j++) {
Staff * allStaffMember =[[Staff alloc] init];
allStaffMember=[self.allStaffArray objectAtIndex:j];
//if the workingStaffMember id is not equal to the allStaffMember id then they are not working today at the hotel
if ([workingStaffMember.staff_id intValue] != [allStaffMember.staff_id intValue ]) {
//set boolean to NO - they are not working today
allStaffMember.isWorking=NO;
//add the staff id to idArray
[idArray addObject:allStaffMember.staff_id];
//loop through the objs
for (NSString * obj in idArray)
{
//add the staff id to the noDuplicatesArray if it already exists
if (![noDuplicatesArray containsObject:obj])
{
[noDuplicatesArray addObject: obj];
//if it doesnt already exist in the noduplicatesArray it means we have not passed it already, add the staff object to the fullArray
[fullArray addObject:allStaffMember];
}
}
}
}
}
//The final array that already contains the working staff members, but the staff who are not working today are to be added to the end.
[self.workingStaffArray addObjectsFromArray:fullArray];
}
包含了今天所有工作人员以及在酒店工作的所有工作人员。
这一行;
self.workingStaffArray
让我失望,但欢迎提供更好的解决方案。
提前致谢:)
编辑 :问题似乎是因为if ([workingStaffMember.staff_id intValue] != [allStaffMember.staff_id intValue ])
有一个实例,每个工作人员都会被添加。例如,我希望结果基于以下示例;
loop through all staff
所有Staff数组包含(使用默认BOOL);
Working array contains:
Staff{
staff_name = @"Charlie";
staff_id = @"121"
isWorking= YES;
}
Staff{
staff_name = @"Sam";
staff_id = @"122"
isWorking= YES;
}
Staff{
staff_name = @"BEN";
staff_id = @"124"
isWorking= YES;
}
finalArray看起来像这样:
Staff{
staff_name = @"Charlie";
staff_id = @"121"
isWorking= YES;
}
Staff{
staff_name = @"Sam";
staff_id = @"122"
isWorking= YES;
}
Staff{
staff_name = @"BEN";
staff_id = @"123"
isWorking= YES;
}
Staff{
staff_name = @"PETER";
staff_id = @"124"
isWorking= YES;
}
Staff{
staff_name = @"Louis";
staff_id = @"125"
isWorking= YES;
}
但目前的实施情况给出了:
Staff{
staff_name = @"Charlie";
staff_id = @"121"
isWorking= YES;
}
Staff{
staff_name = @"Sam";
staff_id = @"122"
isWorking= YES;
}
Staff{
staff_name = @"BEN";
staff_id = @"123"
isWorking= YES;
}
Staff{
staff_name = @"PETER";
staff_id = @"124"
isWorking= NO;
}
Staff{
staff_name = @"Louis";
staff_id = @"125"
isWorking= NO;
}
* EDIT2 * 我的结果不正确。只是为了清楚,以防万一很难理解:我有两个数组。一个包含今天工作的员工,一个包含酒店的所有员工。我想将两者合并为一个Staff对象数组。这个数组包含了今天工作的所有员工,以及那些今天不工作的员工。第三个数组将包含所有工作人员,但唯一的区别是他们将具有适当的BOOl值Staff{
staff_name = @"Charlie";
staff_id = @"121"
isWorking= YES;
}
Staff{
staff_name = @"Sam";
staff_id = @"122"
isWorking= YES;
}
Staff{
staff_name = @"BEN";
staff_id = @"123"
isWorking= YES;
}
Staff{
staff_name = @"Charlie";
staff_id = @"121"
isWorking= NO;
}
Staff{
staff_name = @"Sam";
staff_id = @"122"
isWorking= NO;
}
Staff{
staff_name = @"BEN";
staff_id = @"123"
isWorking= NO;
}
Staff{
staff_name = @"PETER";
staff_id = @"124"
isWorking= NO;
}
Staff{
staff_name = @"Louis";
staff_id = @"125"
isWorking= NO;
}
答案 0 :(得分:4)
首先实现isEqual
对象的Staff
方法:
- (BOOL)isEqual:(id)object
{
if (![object isKindOfClass:[Staff class]])
return NO;
Staff *other = (Staff *)object;
return self.staff_id == other.staff_id;
}
其次使用NSMutableSet
来确保您拥有一组唯一的对象:
NSMutableSet *staffSet = [NSMutableSet new];
....
[staffSet addObject:obj];
(我认为这涵盖了您问题中的大多数问题;如果没有,请添加评论)。