-1

I am building a mobile app with swift, and am having some syntax issues as I am not a developer. The structure and logic of the application is really rough and surely incorrect, however we just need something that functions. (It is a school project and my team got no devs).

Anyways, we have a MySQL database that will be used as a middleman between our badge server/admin app, and our mobile app. Currently when you go to https://gatekeeperapp.org/service.php , you will see the current database data, taken by a php script and hosted there as JSON. Currently in Swift I have a struct with a function that takes this JSON data, and maps it to variables. The idea is to then pass these pulled variables into a separate set of functions that will check the pulled long/lat against the mobile devices location, and then return whether they match or not. This value would be updated, re-encoded to JSON, and pushed to a web service that would go about changing the values in the database so the badge server could use them.

Where I am currently I can see that values are being pulled and mapped and I can set a variable in a separate function to the pulled value, but then I can only seem to output this value internally, rather than actually use it in the function. I get a type error saying that the pulled values are of type (). How can I properly use these values? Ultimately I think I would want to convert the () to a double, so I could properly compare it to the Long/Lat of the device, and then will need to re-encode the new values to JSON.

Swift Code -- struct function

Swift code -- JSON struct

Swift code -- using pulled data

1 Answers1

0

Your closure is called asynchronously, which means that the outer function where you are expecting to use the values has already returned by the time the closure is called. Instead, you probably need to call some other function from the closure, passing the values you've received.

class MyClass {
    func fetchUserData() {
        UserData().fetchUser { [weak self] user, error in
            DispatchQueue.main.async {
                if let user = user {
                    self?.handleSuccess(userID: user)
                } else if let error = error {
                    self?.handleError(error)
                }
            }
        }
    }

    private func handleSuccess(userID: String) {
        print(userID)
        // Do something with userID. Maybe assign it to a property on the class?
    }

    private func handleError(_ error: Error) {
        print(error)
        // Handle the error. Maybe show an alert?
    }
}
dalton_c
  • 6,876
  • 1
  • 33
  • 42