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

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!

   

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)
        }
    }
}

1      

Save 50% in my Black Friday sale.

SAVE 50% To celebrate WWDC22, 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!

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.