Alamofire / Class / Variables problem


I am hoping somebody could tell me what I am doing wrong here, apologies if I get some simple things wrong, I’m a complete novice with xcode.

I have created a custom class, within this custom class I have created some variables and a method that uses Alamofire to call a webservice and return a JSON stream.

On my view controller, I declare a new instance of the class and call the alamofire method, the purpose of this method is to retrieve the JSON stream and elements to the class variables. I then want to access the class variables from my new instance.

This is where I am running in to my problem. The alamofire call is done with a switch statement (based on the guide found on codewithchris). If I set my variable to a static string, within the method but outside the scope of the alamofire switch, I can access the variable from my view controller and the expected value is available, however, if I set the variable to be a result from the JSON stream inside the switch statement, when I access the variable from my view controller, it is empty.

I have ensured the data is being correctly passed to the variable, it is being lost at some point and I am confident I have narrowed it down to an issue within the switch statement.

Any pointers much appreciated.

My Class:

import Foundation
import SwiftyJSON
import Alamofire

class PARTS {
//Class for dealing with Part data.

let part: String
var desc: String
var stockQty = 0

init(_ part: String) {
    self.part = part
    self.desc = ""
    self.stockQty = 0
func getPartJSON() {
    //Variable setting test
    self.desc = "I have set the description manually"

//If I set the variable here, when I access it on my view controller, it works fine.

    AF.request("myserverPath").responseJSON {
        response in switch response.result {
                case .success(let value):
                    //JSON Stream
                    let json = JSON(value)

                    //Pull value from JSON
                    if let pinNumber = json[0, "partNumber"].string {
                        //Print in debug window
                        debugPrint(json[0, "Part_Description"].string!)
                    let curStock = json[0, "TotalUsage"].intValue
                    self.stockQty = curStock
                case .failure(let error):

            } //End of Alamofire request


My view controller function:

func textFieldShouldReturn(_ textField: UITextField) -> Bool {
    //var txtEntered:String? = ""
    let txtEntered = self.serialInput.text
    if txtEntered != nil {
        //Create and intance of GPIN Class
        let part: GPIN = GPIN(txtEntered!)
        let partDesc = part.desc
    else {
        debugPrint("No data entered ")
    return true

Hi Ian,

Seems to me you are not passing the results of the JSON pull into an array which you can access later. Or am I missing something here, entirely possible!


Hi Mark,

Ah, ok, it is far more likely that I am missing something - quite possibly an array! I will give it a try, thank you!