Search code examples
swiftalamofire

What is the best practice of handling Alamofire response If I only need one string from Json


I have a question about handling response of Alamofire json, I need some advice. I use SwiftyJSON to parse json. Returned json is like that;

        ResultCode = "122";
    ResultText = "The content of this sample could not be recognized.";
    TransactionDetails =         {
        IsComplete = 0;
        TransactionId = 9398;
        Transactionstate = OnGoing;
        Transactiontype = Authentication;
    };
    VoiceDetails =         {
        DiscardedSpeech = 1;
        ProcessResult = None;
        SpeechResult = BadContentSpeech;
        TotalSpeech = 1;
    };
};

}

If I only need TransactionState from json. Is it an acceptable way to use closure and pass this value like that?

if let strState : String = swiftyJsonVar["AuthVoicePrintData"]["TransactionDetails"]["TransactionState"].string
      {
          completionHandler(strState)
          return
      }
      completionHandler("Something went wrong")

Or even If i need only TransactionState , still I should use something like ObjectMapper` and map all these values in a model class to use where I need? Thank you.


Solution

  • Is it an acceptable way to use closure and pass this value like that?

    Yes , it's the only right way to handle asynchronous calls

    Even If i need only TransactionState , still I should use something like ObjectMapper` and map all these values in a model class

    No you don't need them nor Codable , using swifty json here is the shortest path also jsonSerialization can do the job