UPGRADE YOUR SKILLS: Learn advanced Swift and SwiftUI on Hacking with Swift+! >>

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      

BUILD THE ULTIMATE PORTFOLIO APP Most Swift tutorials help you solve one specific problem, but in my Ultimate Portfolio App series I show you how to get all the best practices into a single app: architecture, testing, performance, accessibility, localization, project organization, and so much more, all while building a SwiftUI app that works on iOS, macOS and watchOS.

Get it on Hacking with Swift+

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.