NSMutableArray check if object already exists

前端 未结 9 1391
傲寒
傲寒 2020-12-25 10:17

I am not sure how to go about this. I have an NSMutableArray (addList) which holds all the items to be added to my datasource NSMutableArray.

I now

相关标签:
9条回答
  • 2020-12-25 10:54

    There is a very useful method for this in NSArray i.e. containsObject.

    NSArray *array;
    array = [NSArray arrayWithObjects: @"Nicola", @"Margherita",                                       @"Luciano", @"Silvia", nil];
    if ([array containsObject: @"Nicola"]) // YES
    {
        // Do something
    }
    
    0 讨论(0)
  • 2020-12-25 10:55

    I found a solution, may not be the most efficient of all, but atleast works

    NSMutableArray *add=[[NSMutableArray alloc]init];
    
    for (Item *item in addList){
            if ([appDelegate.list containsObject:item])
                {}
            else
                [add addObject:item];
    }
    

    Then I iterate over the add array and insert items.

    0 讨论(0)
  • 2020-12-25 10:56

    Have a look at NSSet. You can add objects and the object will only be added if the object is unique. You can create a NSSet from an NSArray or vise versa.

    0 讨论(0)
提交回复
热议问题