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

SOLVED: Day35: If you intend to pass a closure into a view’s initializer for later use, Xcode will force you to mark it as @escaping .

Forums > 100 Days of SwiftUI

This sentence is out of the hint-section from Day35-Challenge-Day:

If you intend to pass a closure into a view’s initializer for later use, Xcode will force you to mark it as @escaping .

Can someone give me an example for this case? Actually I didn't need such a closure for the challenge but I'm interested how it would look like. Thanks in advance!

2      

Crab asks about @escaping:

Can someone give me an example for this case?
...snip... I'm interested how it would look like.

Create a new project and paste the following example. Please study and report back what you learned.

//  EscapingClosureView.swift
//  Created by Obelix on 5/6/22.

import SwiftUI
// Day 35 Hint
// If you intend to pass a closure into a view’s initializer for later use,
// Xcode will force you to mark it as @escaping.
// This means “will be used outside of the current method.”
struct SortedBeatles: View {
    var sortDirection: (String, String)->Bool  // <- Tell me how to sort
    var liverpoolLads: [String]                // <- Tell me what to sort

    // pass a closure into a view's initializer
    // I need a closure that takes two Strings, and returns a Bool
    // Where are these defined? In the other view, not here.
    // They are "packaged" with the closure. They "escaped" the previous struct!
    init(direction: @escaping (String, String)->Bool, performers: [String]) {
        sortDirection = direction
        liverpoolLads = performers
    }

    var body: some View {
        ForEach( liverpoolLads.sorted(by: sortDirection), id:\.self) { Text( $0 ) }
    }
}

struct EscapingClosureView: View {
    // Define two closures. Functions we'll use later on.
    let sortDecending = { (n1:String, n2:String) in n1 > n2 }
    let sortAscending = { (n1:String, n2:String) in n1 < n2 }
    let beatles = ["john", "paul", "george", "ringo", "obelix"]
    @State private var isAscending = true

    var body: some View {
        VStack{
            Form {
                Section("Pre sort") {
                    ForEach( beatles, id: \.self) { Text($0) }
                }
                Toggle("Sort Ascending", isOn: $isAscending)
                Section("Sorted Beatles") {
                    // NOTE: Passing in a closure to the view's initializer
                    SortedBeatles(direction: isAscending ? sortAscending : sortDecending, performers: beatles)
                }
            }.font(.caption).animation(.easeInOut(duration: 1.0), value: isAscending)
        }
    }
}

3      

Hacking with Swift is sponsored by RevenueCat

SPONSORED Take the pain out of configuring and testing your paywalls. RevenueCat's Paywalls allow you to remotely configure your entire paywall view without any code changes or app updates.

Learn more here

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.