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

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

Forums > macOS

@matt  

Hello,

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.

3      

@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

4      

Hacking with Swift is sponsored by Essential Developer

SPONSORED Join a FREE crash course for mid/senior iOS devs who want to achieve an expert level of technical and practical skills – it’s the fast track to being a complete senior developer! Hurry up because it'll be available only until April 28th.

Click to save your free spot now

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.