0

I have a struct that gets data from my Firebase DB and populate a number of constants, this is to populate a TabelView on a seperate VC. initially I was trying to calculate distance from location in the tableViewCell class, but I want to be able to sort by distance, and read something about calculating the distance within the struct.

So I have moved my calculation to the struct asa func, and I am getting an error stating 'self' used before all stored properties are initialzed.

This is my struct and I have commented on the line where the error appears. What am I missing here?

import Foundation
import FirebaseDatabase
import CoreLocation

struct newTracks {

//Declerations
var locationManager = CLLocationManager()
let name: String!
let lat: Double!
let lon: Double!
let countryImage: String!
let link: String!
let ref: FIRDatabaseReference?
let distance: Double


//Initialize
init(name: String, trackId: Int, postcode: String, trackType: String, trackURL: String, locID: Int, lat: Double, lon: Double, phoneNumber: String, email: String, rating: Double, numrating: Double, totalrating: Double, countryImage: String, link: String, distance: Double) {
    self.name = name
    self.ref = nil
    self.lat = lat
    self.lon = lon
    self.countryImage = countryImage
    self.link = link
    self.distance = distance
 }

//Initialize data from Firebase
init(snapshot: FIRDataSnapshot) {
    let snapshotValue = snapshot.value as! [String: AnyObject]
    name = snapshotValue["name"] as! String
    lat = snapshotValue["lat"]as! Double
    lon = snapshotValue["long"]as! Double
    ref = snapshot.ref
    countryImage = snapshotValue["country"] as! String
    link = snapshotValue["link"] as! String
    distance = getDistance() //error on this line -- 'self' used before all stored properties are initialzed
 }

//calculate distance from current location to destination location
func getDistance() -> CLLocationDistance {
    let currentLat = self.locationManager.location!.coordinate.latitude
    let currentLon = self.locationManager.location!.coordinate.longitude
    let myLocation = CLLocation(latitude: currentLat, longitude: currentLon)
    let loc = CLLocation(latitude: self.lat, longitude: self.lon)
    let distanceInMiles = round(myLocation.distance(from: loc) / 1609.34)
    return distanceInMiles
}

func toAnyObject() -> Any {
    return [
        "name": name,
        "lat": lat,
        "lon": lon,
        "countryImage": countryImage,
        "link": link,
        "distance": distance
    ]

 }

}
1

1 Answer 1

1

You cant call to instance function during initialization, either mark your function static (equal to class function) or move it to another class/struct, or try move the function inside the initialization method

Sign up to request clarification or add additional context in comments.

3 Comments

if I change the func to static func I now get 3 errors within the function saying 'Instance member locationManager cannot be used on newTracks'
similar to class function, static function also cant access instance member, either create new CLLocationManager() inside the function, or move the fileprivate let locationManager = CLLocationManager() outside the struct scope (make it global for the file)
ok managed to get it working by moving the calculation to the init method, thank you

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.