WWDC24 SALE: Save 50% on all my Swift books and bundles! >>

SOLVED: Why doesn't the parameter name matter in this function's signature?

Forums > SwiftUI

I'm trying to marry up what I see in the developer documentation with what I have just written, just so's I fully understand next time I write something like:

ForEach(expenses.items, id: \.name) { item in
                    Text(item.name)
                }
                .onDelete(perform: removeItems)

.
.
.
func removeItems(parameter1 offsets: IndexSet) {
        expenses.items.remove(atOffsets: offsets)
    }

It doesn't seem to matter what symbol, or underscore, I substitute for parameter1, the function removeItems is successfully performed by the ForEach.onDelete(perform:) regardless of the parameter name in its signature.

Specifically, my question is about writing the signature of the function that is to be performed :

When I see

 @inlinable public func onDelete(perform action: ((IndexSet) -> Void)?) -> some DynamicViewContent

can I assume that I can, since my removeItemsfunction is being used as a closure - all Swift functions are closures, after all - that as long as it accepts one argument of type IndexSet, it doesn't matter what name I give to the corresponding parameter.

Actually, in posting this, I think I've answered my own question. Mostly.

I assume that, since there is no overloading of perform, and as it is being invoked as a closure, then there is no choice to be made between different signature "flavours" of perform and thus a parameter name is not important and any will do - so long as there is one that the function can use.

2      

Yeah, what matters is the function signature, not so much the parameter names. So you have to supply a function with a signature of (IndexSet) -> Void but what you call that IndexSet parameter is totally up to you.

2      

Save 50% in my WWDC sale.

SAVE 50% To celebrate WWDC24, all our books and bundles are half price, so you can take your Swift knowledge further without spending big! Get the Swift Power Pack to build your iOS career faster, get the Swift Platform Pack to builds apps for macOS, watchOS, and beyond, or get the Swift Plus Pack to learn advanced design patterns, testing skills, and more.

Save 50% on all our books and bundles!

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.