NEW: Learn to build the incredible iOS 15 Weather app today! >>

SOLVED: Can someone please clarify this? - If a method has the same signature as a closure parameter, we can use the method instead.

Forums > 100 Days of SwiftUI

This was in one of the answers in the Day 31 review questions.

If a method has the same signature as a closure parameter, we can use the method instead.

I basically get that methods are functions like closures and it seems like Paul is saying if a method has the same type as a closure paramater, we can use a method instead, but does someone have an example to illustrate this point?

   

Sorting an array is probably the easiest way to demonstrate this.

import Foundation

let names = ["Charlotte", "Shauna", "Mildred", "Linton", "Jack", "Sonny"]

//sorted(by:) takes a closure (String, String) -> Bool
let sortedNames1 = names.sorted(by: { name1, name2 in
    name1 < name2
})
print(sortedNames1)
//["Charlotte", "Jack", "Linton", "Mildred", "Shauna", "Sonny"]

//function signature here is (String, String) -> Bool
func namesInAscendingOrder(name1: String, name2: String) -> Bool {
    name1 < name2
}
//we can pass the function instead of a closure
let sortedNames2 = names.sorted(by: namesInAscendingOrder)
print(sortedNames2)
//["Charlotte", "Jack", "Linton", "Mildred", "Shauna", "Sonny"]

   

Great, thanks.

   

Here's another example. This shows how a function requires you to provide:

  1. Two integers
  2. A function

What function does it require? The signature doesn't specify what the function needs to do. It only specifies the function needs to accept two integer, then return an integer. You can provide any function you want. It just has to conform to these rules.

But this example also shows several ways to provide that required function!

Three examples show how the function is passed in as a parameter. The other two examples show how to pass in the function using trailing closure syntax.

Paste this code into Playgrounds. See if this adds to the excellent answer posted above.

func applyFunction(to lhs: Int, and rhs: Int, thisFunction someFunction: (Int, Int) -> Int ) -> Int {
    // pass in the left hand side and right hand side values.
    // then perform the function with those values passed in.
    // I have no idea what the function does, but I'll run it anyway.
    return someFunction(lhs, rhs) //
}

func sekretFunction(x: Int, y: Int) -> Int { x * y } // shhhh! sekret!

let answer01 = applyFunction(to: 22, and: 20, thisFunction: { $0 + $1 } )    // provide a closure via the initializer
let answer02 = applyFunction(to: 40, and:  2) { $0 * 2 + $1 } // provide a closure using trailing closure syntax
let answer03 = applyFunction(to: 44, and:  2, thisFunction: sekretFunction ) // provide a function via initializer
let answer04 = applyFunction(to: 20, and:  5) { sekretFunction(x: $0, y: $1) } // provide a function via closure
let answer05 = applyFunction(to: 60, and: 20, thisFunction: / ) // Divide! it's a function that takes two ints and returns an int

   

Different Signatures

Not totally related to your question. But take a look at readability! If an application requires you to write a function that applies another function to two integers, you might consider rearranging the variables in the signature.

Put the function first, followed by the two integers. In my example, you lose the ability to use trailing closure syntax. However, do you agree the final version reads much more like an English sentence?

func sekretFunction(x: Int, y: Int) -> Int { x * y } // shhhh! sekret!

// Reorder the parameters. 
func applyFunction(_ someFunction: (Int, Int) -> Int , to lhs: Int, and rhs: Int) -> Int {
    someFunction(lhs, rhs) // apply the function to the two variables.
}

// Do you agree? This reads much more like a sentence?
let answer01 = applyFunction(sekretFunction, to: 10, and: 30)  // Alternate signature. 

1      

Thanks for the additional examples:

This was the most interesting: let answer05 = applyFunction(to: 60, and: 20, thisFunction: / ) // Divide! it's a function that takes two ints and returns an int

I hadn't thought of / as a function.

And yes, the last line in the last post definitely makes it easier to read.

   

Hacking with Swift is sponsored by Sentry

SPONSORED With Sentry’s error and performance monitoring for iOS, you see mobile vitals that actually matter, can solve any latency issues quickly, and learn how each release is performing over time.

Learn More

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

Reply to this topic…

You need to create an account or log in to reply.

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.