0

I'm trying to remove an object from array of objects

I tried different scenarios, but instead of removing item it has been added 2 times to the array, I'm going out of my mind and couldn't find any reason for that

 let pages = surveyData.pagerItems; // Pages is a model which contains an array of another model called question
 let questions = self.pages[indexPath.section].questionItems.filter{$0.id != questionId};
 self.pages[indexPath.section].questionItems = questions;

I tried to remove all questions items first also before assigning it to pages object

self.pages[indexPath.section].questionItems.removeAll();
self.pages[indexPath.section].questionItems = questions;

also tried this

self.pages[sourceIndex.section].questionItems.remove(at: sourceIndex.row)

Also when I try to insert object, it adds more than once.

please help.

3 Answers 3

1

Use below code, I hope it may work:-

self.pages[indexPath.section].questionItems.removeAll(keepingCapacity: false)
self.pages[indexPath.section].questionItems.append(questions)
Sign up to request clarification or add additional context in comments.

4 Comments

Thanks, but didn't work. I added print after removeAll print("count(self.pages[indexPath.section].questionItems.count)"); count is still the same
First check which section of array you want to remove, then print the output
in all cases old array doen't change it always append to it even if I remove all objects or removeAt index. I don't know why.
This is TwoD array, and above code is working for me and according to your given question. You should go through the code.
0

To remove an object from array:

var array = ["First Object", "Second Object", "Third Object"]

if let index = array.index(of:"Third Object") 
{
    array.remove(at: index)
}

1 Comment

Thanks, didn't work too, I tried almost everything. I don't know if it has something to do with send by reference.
0

I have tried your scenario, its working fine. I created a Modal named page having array variable as questions. And fill values as given below.

class Page {
    var questions = [String]();
}

Getting sample values filled.

    let page1 = Page();
    page1.questions.append("P1->First Element");
    page1.questions.append("P1->Second Element");
    page1.questions.append("P1->Third Element");
    page1.questions.append("P1->Fourth Element");
    page1.questions.append("P1->Fifth Element");

    let page2 = Page();
    page2.questions.append("P2->First Element");
    page2.questions.append("P2->Second Element");
    page2.questions.append("P2->Third Element");
    page2.questions.append("P2->Fourth Element");

    let page3 =  Page();
    page3.questions.append("P3->First Element");
    page3.questions.append("P3->Second Element");
    page3.questions.append("P3->Thirs Element");


    var pages = [Page]();        
    pages.append(page1);
    pages.append(page2);
    pages.append(page3);

Now we will see output before removing item and after removing item.

    print("p1.question \(pages[0].questions)");
    print("p2.question \(pages[1].questions)");
    print("p3.question \(pages[2].questions)");

    pages[1].questions.remove(at: 2);

    print("p1.question \(pages[0].questions)");
    print("p2.question \(pages[1].questions)");
    print("p3.question \(pages[2].questions)");

Now you can see output as expected.

p1.question ["P1->First Element", "P1->Second Element", "P1->Third Element", "P1->Fourth Element", "P1->Fifth Element"]
p2.question ["P2->First Element", "P2->Second Element", "P2->Third Element", "P2->Fourth Element"]
p3.question ["P3->First Element", "P3->Second Element", "P3->Thirs Element"]

p1.question ["P1->First Element", "P1->Second Element", "P1->Third Element", "P1->Fourth Element", "P1->Fifth Element"]
p2.question ["P2->First Element", "P2->Second Element", "P2->Fourth Element"]
p3.question ["P3->First Element", "P3->Second Element", "P3->Thirs Element"]

As you can see third Item from page at index 1 has been removed successfully. I think you are confusing between let and var. In swift let is immutable object where as var is mutable one.

1 Comment

Thanks all, I had to do the removing, inserting, appending in the model itself.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.