Iphone 将对象插入NSMutableArray

Iphone 将对象插入NSMutableArray,iphone,objective-c,cocoa-touch,nsmutablearray,Iphone,Objective C,Cocoa Touch,Nsmutablearray,目前,我编辑了一个委托函数,该函数将练习对象添加到NSMutableArray中。但是,我不想添加重复的对象,相反,如果该对象已经在数组中,我只想访问该特定对象 这是我的密码: - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath { UITableViewCell *cell = [tableView cellForRowAtIndexPath:index

目前,我编辑了一个委托函数,该函数将练习对象添加到NSMutableArray中。但是,我不想添加重复的对象,相反,如果该对象已经在数组中,我只想访问该特定对象

这是我的密码:

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {

    UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
    NSString *str = cell.textLabel.text; // Retrieves the string of the selected cell.

    Exercise *exerciseView = [[Exercise alloc] initWithExerciseName:str];
    WorkoutManager *workoutManager = [WorkoutManager sharedInstance];

    if (![[workoutManager exercises] containsObject:exerciseView]) {
        [[workoutManager exercises] insertObject:exerciseView atIndex:0];
        [self presentModalViewController:exerciseView animated:YES];
        NSLog(@"%@", [workoutManager exercises]); 
    }
    else {
        [self presentModalViewController:exerciseView animated:YES];
        NSLog(@"%@", [workoutManager exercises]); 
    }
}
我原以为这会管用,但是,当我运行代码并记录数组时,它显示当我单击同一个单元格时,创建了两个不同的对象。有什么帮助吗?

每次打电话时

Exercise *exerciseView = [[Exercise alloc] initWithExerciseName:str];
它创建一个新的(不同的)exerciseView对象。因此,尽管练习名称可能与练习列表中某个练习对象的名称相同,但它是一个全新的对象,因此当您调用
containsObject
时,结果将始终为false,并且新对象将添加到数组中

也许您应该在您的训练管理器中存储NSString
exerciseName
的列表,而不是每次呼叫时

Exercise *exerciseView = [[Exercise alloc] initWithExerciseName:str];
它创建一个新的(不同的)exerciseView对象。因此,尽管练习名称可能与练习列表中某个练习对象的名称相同,但它是一个全新的对象,因此当您调用
containsObject
时,结果将始终为false,并且新对象将添加到数组中


也许你应该在你的训练管理器中存储一个NSString
exerciseName
列表?

我认为这是你的罪魁祸首:

Exercise *exerciseView = [[Exercise alloc] initWithExerciseName:str];
您每次都创建一个新对象,因此从技术上讲,它不在数组中。containsObject方法只是在数组中迭代,并对每个对象调用isEqual。我没有对此进行测试,但从理论上讲,在自定义练习对象中,可以重写isEqual方法来比较练习名称属性,如果它们匹配,则返回true。请看,当您使用containsObject时,所有内容都必须匹配,因此即使所有属性都相同,objectid也不相同

轻松修复,无需查看您的练习实施:

Exercise *exerciseView = nil;

For(Exercise *exercise in [[WorkoutManager sharedInstance] exercises]){
    if(exercise.exerciseName == str) {
        exerciseView = exercise;
        break;
    }
}

if(exerciseView == nil) {
    exerciseView = [[Exercise alloc] initWithExerciseName:str];
    [[workoutManager exercises] insertObject:exerciseView atIndex:0];
}

[self presentModalViewController:exerciseView animated:YES];

希望这有助于解释为什么会发生这种情况。我没有测试这段代码,因为有一些缺失的片段,但你应该知道。玩得开心

我认为这是你的罪魁祸首:

Exercise *exerciseView = [[Exercise alloc] initWithExerciseName:str];
WorkoutManager *workoutManager = [WorkoutManager sharedInstance];

Exercise *temp = [[Exercise alloc] initWithExerciseName:str];
for(id temp1 in workoutManager)
{
    if( [temp isKindOfClass:[Exercise class]])
    {
        NSLog(@"YES");
        // You Can Access your same object here if array has already same object
    }
}

 [temp release];
 [workoutManager release];
您每次都创建一个新对象,因此从技术上讲,它不在数组中。containsObject方法只是在数组中迭代,并对每个对象调用isEqual。我没有对此进行测试,但从理论上讲,在自定义练习对象中,可以重写isEqual方法来比较练习名称属性,如果它们匹配,则返回true。请看,当您使用containsObject时,所有内容都必须匹配,因此即使所有属性都相同,objectid也不相同

轻松修复,无需查看您的练习实施:

Exercise *exerciseView = nil;

For(Exercise *exercise in [[WorkoutManager sharedInstance] exercises]){
    if(exercise.exerciseName == str) {
        exerciseView = exercise;
        break;
    }
}

if(exerciseView == nil) {
    exerciseView = [[Exercise alloc] initWithExerciseName:str];
    [[workoutManager exercises] insertObject:exerciseView atIndex:0];
}

[self presentModalViewController:exerciseView animated:YES];
希望这有助于解释为什么会发生这种情况。我没有测试这段代码,因为有一些缺失的片段,但你应该知道。玩得开心

WorkoutManager *workoutManager = [WorkoutManager sharedInstance];

Exercise *temp = [[Exercise alloc] initWithExerciseName:str];
for(id temp1 in workoutManager)
{
    if( [temp isKindOfClass:[Exercise class]])
    {
        NSLog(@"YES");
        // You Can Access your same object here if array has already same object
    }
}

 [temp release];
 [workoutManager release];
希望这能帮助你

希望这能帮助你