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

SOLVED: Project 14 Question

Forums > 100 Days of SwiftUI

Dear guys,

I have a couple of questions:

1 - Can somebody explain me the purpose of the following code in Bucketlist?

var onSave: (Location) -> Void

2 - When running the project on simulator, I noticed a highly elevated number of purple warnings. Why? Is it possible to resolve them?

Thank you

2      

var onSave: (Location) -> Void

tells you that the onSave variable is a closure that takes a Location parameter and returns nothing.

So you can do something like:

onSave = { (location: Location) in
    //do something with location
}

or:

func saveTo(location: Location) {
    //do something with lication
}

onSave = saveTo

I'm posting this from my phone, so I will let somebody else who has access to Xcode answer your second question, but it would probably be very useful if you posted some of the code that was causing the purple errors.

2      

@andreaSara wonders about closures:

Can somebody explain me the purpose of the following code in Bucketlist?

    var onSave: (Location) -> Void

Patrick provides the correct answer! But I'd like to add some commentary.

In programming languages everyone seems comfortable with variables that hold Integers, Doubles, Strings, and Booleans. If you have the following code in your program, no one thinks twice about what you've declared:

// This is standard stuff in Swift and other languages
    let favoriteSinger     = "Taylor Swift"  // <- Store a string
    let favoriteNumber     = 42              // <- Store an integer
    let thisIsNotPi        = 3.1512929       // <- Store a double
    let isContrivedExample = true            // <- Store a boolean
    let famousProgammers:    [Programmer]    // <- Store a collection of programmer objects

But the line you quote above gives new programmers absolute fits! Why?

I think it's because new programmers are NOT comfortable with the concept that Swift allows variables to hold more than just standard data and arrays.

Store a what?

The onSave variable in your example doesn't hold a boolean. It doesn't hold a string. What does it hold? It's a strange concept to many, but the code is telling Swift that the variable onSave holds a function! Moreover, the code says the function has a specific function signature. This signature requires the function to consume a Location object (defined somewhere else...). AND the function doesn't return any value when it completes its work.

What does the function do?

We don't know and we don't care.

As long as you provide a function that accepts a Location and returns nothing, the variable onSave can hold your function. It's similar to how a variable that holds an Integer can hold a 42, a 0, or 1_000_042. The only requirement is the number must be an integer.

onSave doesn't care what function you provide. The only rule is the function must consume a Location, and it must not return any other value.

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.