@matt is lost and wants to know where to go....
When the book says "we need to create an instance of our Location
struct and add it to our locations array:"
I don't understand how to proceed.
Somewhere in your project you have a template of a Location
object. Here's a simplified mockup.
// Simple location. Your location template will contain more data!
struct Location: Identifiable {
id = Int.random()
street: String // Simple location needs a street address
postalCode: String // Simple location needs a postal code.
}
This code is just a template of what a Location
in your application will hold. The template is just a recipe that you use to make an actual location.
In your code's logic, you'll want a collection of many locations. Per @twoStraw's directions, you'll have an array named locations
. Somewhere in your view logic, or a data model you'll have this definition:
var locations = [Location]() // Define an empty collection of locations. There's nothing here yet.
To add actual locations, you need to use the Location
recipe and create them. In Swift this is called creating an instance of a Location
.
// Use the Location template to create an actual location to use in your application
let myHouse = Location(street: "10 Downing Street", postalCode: "SW1A 2AA") // Not my actual home address!
You can make several locations using the template. Go wild!
To add these to your locations
collection, you probably know to use the append
command for arrays. Like this:
locations.append(myHouse) // <-- add a location to the locations array