2

Hey below you find my source code. What i wanna do is:

After the Json is extracted I want that it will be accessable in a other view... Please help me...

I Mean the extracted Value: "\n (users.Name)"

So I want simply the response data into a other view...

struct User: Codable {
let Name: String
}

let url = URL(string: "http://192.168.178.26/iso/loginserv.php")
                guard let requestUrl = url else { fatalError() }

                var request = URLRequest(url: requestUrl)
                request.httpMethod = "POST"

                let postString = "user=\(self.user)&pass=\(self.pass)";

                request.httpBody = postString.data(using: String.Encoding.utf8);

                let task = URLSession.shared.dataTask(with: request) {
                    (data, response, error) in

                    if let error = error {
                        print("Error took place \(error)")
                        return
                    }

                    guard let data = data else {return}

                    do{
                        let users = try JSONDecoder().decode(User.self, from: data)
                        print("Response data Name: \n \(users.Name)")


                        if !(data.isEmpty) {
                            self.signedIn = true
                        }

                    }catch let jsonErr{
                        print(jsonErr)
                    }

                }


                task.resume()

All i want is to get the marked thing into another view...

1
  • An important question is: How do you want this new view to work? Are you looking to have the view do a Navigation push, like tapping on a detail indicator, or are you wanting to overlay a custom view on your existing view? Commented Feb 4, 2020 at 0:54

2 Answers 2

1

What you could do is use Singleton design pattern.

Basically you create a class that is instanced a single time only, with a global static property. That property will be used to pass info from one view to another.

So, first create a Singleton class (as a simple Swift file) and define a static instance, like this:

class Singleton {

      static let instance = Singleton()

      private init() {

      }
}

Now, create and initialize the variable you want to share between views, for example the string name:

class Singleton {

      static let instance = Singleton()

      private init() {

      }

      var name = ""

}

Now, in your code, attribute users.Name to it:

                do{
                    let users = try JSONDecoder().decode(User.self, from: data)
                    print("Response data Name: \n \(users.Name)")

                    Singleton.instance.name = users.Name

                    if !(data.isEmpty) {
                        self.signedIn = true
                    }

                }catch let jsonErr{
                    print(jsonErr)
                }

Now, to access this info in another view, just use Singleton.instance.name.

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

1 Comment

This is way long and complex
0

Use a struct like this

struct structFittings {
    static var collectedWorks: Bool = false
    static var collected: String = "notcollected"
    static var failclicked: Bool = false
}

Access like this

structFittings.collectedWorks 

Any where in the app

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.