Swift Alamofire SwiftyJson

Description of Requirement:

  A GET request url, returns json string need to be resolved.

Proceed as follows:

  The first is the use Alamofire initialize a url, to note here is not to custom parameters, such as to replace a parameter with% @. You should be used Alamofire own set of parameters:

let parameters: Parameters = ["sort": "string"]
Alamofire.request(url,parameters:parameters).responseJSON{response  in
    //your code here  
}

   Next json string is parsed, it is convenient to use SwiftyJson avoided cast between types, we have completed the framework instead of

            switch response.result {
            case .success( _):
                if let data = response.data {
                    do{
                        let json = try JSON(data: data)
                        // result means that the first key value json, and the value is an array way back out, where required in accordance with the actual situation of their own
                        let subString_sick = json["result"].array![0][0].string!.split(separator: ";")
                        // handle another
                        let subString_advice = json["result"].array![0][1].string!.split(separator: ";")
                        // integration, saved in a global variable ease of use elsewhere
                        let arraySickAndAdvice = [subString_sick,subString_advice]
                        
                        self.dictionarySickLabel[sickTag] = arraySickAndAdvice
                        
                    }catch{}
                    
                }
            case .failure(let error):
                print("\(error)")
            }

 About dictionary Swift's initial definition of reference:

    var arraySickLabels: Array<UILabel> = []
    var dictionarySickLabel : Dictionary<String, Array<Any>> = [:]

 When the rest of the needs of the type of call forwarding it

as! [String.SubSequence] // because the variables we use splite get is the type

 

Reference website:

https://github.com/SwiftyJSON/SwiftyJSON

https://github.com/Alamofire/Alamofire/blob/master/Documentation/Alamofire%204.0%20Migration%20Guide.md

https://www.jianshu.com/p/f8c3adb056cf

https://www.jianshu.com/p/ff8c099d23b1

https://www.jianshu.com/p/acf91a4113ff

https://cloud.tencent.com/developer/ask/131211

Guess you like

Origin www.cnblogs.com/JMarshall/p/11491531.html