let lineCoords: [CLLocationCoordinate2D] for marker in markers {…
- User
let lineCoords: [CLLocationCoordinate2D] for marker in markers { lineCoords.append(CLLocationCoordinate2D(latitude: marker.latitude, longitude: marker.longitude)) }
/Users/eboschert/Desktop/Keepers/Programming/TestApp/TestApp/MapViewController.swift:36:28: Constant 'lineCoords' passed by reference before being initialized
- Assistant
The error message is informing you that the constant
lineCoordsis being modified before it has been initialized. Swift constants defined withletmust be initialized before they can be used. Additionally, you can’t append elements to an array constant because constants are immutable—they cannot be changed after they are set.To fix this, you should initialize
lineCoordsas a mutable array (usingvar) before you start appending coordinates to it. Here is a corrected version of your code:var lineCoords = [CLLocationCoordinate2D]() // Making it a variable and initializing as an empty array for marker in markers { lineCoords.append(CLLocationCoordinate2D(latitude: marker.latitude, longitude: marker.longitude)) }This will initialize an empty array of
CLLocationCoordinate2Dstructs and then append new coordinates for each marker inside the loop. Remember that in Swift, if you plan to modify an array (such as adding elements to it), you should declare it withvarrather thanlet.