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

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      

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.