TEAM LICENSES: Save money and learn new skills through a Hacking with Swift+ team license >>

SOLVED: Question on Day55: Deleting from a Core Data fetch request

Forums > 100 Days of SwiftUI

I have a question on day 55 where the method below is used to delete an object from a List made from Core Data.

func deleteBooks(at offsets: IndexSet) {

    for offset in offsets {

        let book = books[offset]

        moc.delete(book)      
    }

    try? moc.save()
}

My question is, why does this have to iterate over the offsets parameter? Doesn't the offsets parameter always have only 1 index to delete? Or, is this because there can be some cases where multiple objects are deleted at the same time?

Thank you, Kiyo

2      

It is the second point you mentioned - where you could delete multiple items in one go.

The function is passed an IndexSet, which could be 0 (nil) to many items, hence you have to interate over the offsets parameter.

3      

@Greenamberred

Thank you very much for your answer! I understand it clearly now :)

Kiyo

2      

Hacking with Swift is sponsored by String Catalog.

SPONSORED Get accurate app localizations in minutes using AI. Choose your languages & receive translations for 40+ markets!

Localize My App

Sponsor Hacking with Swift and reach the world's largest Swift community!

Archived topic

This topic has been closed due to inactivity, so you can't reply. Please create a new topic if you need to.

All interactions here are governed by our code of conduct.

 
Unknown user

You are not logged in

Log in or create account
 

Link copied to your pasteboard.