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

SOLVED: I'm having troubles partway thru Project 5: MultiMap

Forums > macOS



I got to page 309 without troubles, but when it came to filling out the newLocation code on page 311, I must not have understood where to place my code, because I've tried numerous things without success.

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.

Am I supposed to create the struct within the runSearch func in ContentView?

Any assistance is greatly appreciated.


@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


Hacking with Swift is sponsored by Superwall.

SPONSORED Superwall lets you build & test paywalls without shipping updates. Run experiments, offer sales, segment users, update locked features and more at the click of button. Best part? It's FREE for up to 250 conversions / mo and the Superwall team builds out 100% custom paywalls – free of charge.

Learn More

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.