我有一个NSMutableArray
包含 Person 类型的对象。Person 对象包含NSString
*name、NSString
*dateStamp 和NSString
*testScore 参数。我想使用快速枚举做的是检查NSMutableArray
*testResults,看看是否存在具有相同名称参数的对象。
如果是这样,那么我想将 , 中的现有对象替换为NSMutableArray
我将要插入的对象,该对象将具有最新的dateStamp
, 和 testScore 值。如果找到没有匹配名称参数的对象,则只需插入我拥有的对象。
到目前为止,我的代码如下所示:
这是创建我要插入的对象的代码:
Person *newPerson = [[Person alloc] init];
[newPerson setPersonName:newName]; //variables newName, pass, and newDate have already been
[newPerson setScore:pass]; //created and initialized
[newPerson setDateStamp:newDate];
这是我尝试遍历 NSMutableArray 以查看是否已存在具有相同名称参数的对象的代码:
for (Person *checkPerson in personList) { //personList is of type NSMutableArray
if (newPerson.newName == checkPerson.name) {
//here is where I need to insert the code that replaces checkPerson with newPerson after a match has been found
}
else {
personList.addObject(newPerson); //this is the code that adds the new object to the NSMutableArray when no match was found.
}
}
这不是一个非常复杂的问题,但我很困惑如何寻找匹配项,然后在不知道对象所在索引的情况下替换实际对象。