Returning Data from JSON Parse - Swift - ios

So I'm a newbie with Swift, and somewhat with programming in general. I've only been doing it since the first of the year... I'm trying to make a simple app that pulls data from an external JSON file, and inputs it into UILabels. I've gotten the data to pull, and append to an array. From here, it seems to exit the scope and not be useable anywhere else... I've created a struct that is to hold the data, accordingly. As you can see, I've added print markers to see what is going on, visually.
struct GlobalTestimonialData {
var testimonialsText: [String]
var customerNames: [String]
var companyNames: [String]
}
var TestimonialData = GlobalTestimonialData(testimonialsText: [""], customerNames: [""], companyNames: [""])
func getData () {
let requestURL: URL = URL(string: "https://szadydesigns.com/test/mobileapp/testimonials.php")!
let urlRequest = URLRequest(url: requestURL as URL)
let session = URLSession.shared
let task = session.dataTask(with: urlRequest, completionHandler: { (data, response, error) in
let httpResponse = response as! HTTPURLResponse
let statusCode = httpResponse.statusCode
if (statusCode == 200) {
print("File has been downloaded!")
do {
let json = try JSONSerialization.jsonObject(with: data!, options:.allowFragments)
print("JSON Serialized")
if let JSONfile = json as? [String: AnyObject] {
print("JSON Reading")
if let testimonial = JSONfile["testimonial"] as? [String] {
print("Testimonials Read")
TestimonialData.testimonialsText.append(contentsOf: testimonial)
print(TestimonialData.testimonialsText)
print("Inside of loop Testimonial Text Number: \(TestimonialData.testimonialsText.count)")
if let name = JSONfile["name"] as? [String] {
print("Names Read")
TestimonialData.customerNames.append(contentsOf: name)
print(TestimonialData.customerNames)
print("Inside of loop Customers Number: \(TestimonialData.customerNames.count)")
}
if let company = JSONfile["company"] as? [String] {
print("Companies Read")
TestimonialData.companyNames.append(contentsOf: company)
print(TestimonialData.companyNames)
}
print("Companies: \(TestimonialData.companyNames)")
}
print("COMPANIES: \(TestimonialData.companyNames)")
}
print("Companies AGIAN: \(TestimonialData.companyNames)")
}catch {
print("Error with Json: \(error)")
}
print("Companies AGIAN AGAIN : \(TestimonialData.companyNames)")
}
print("Companies AGIAN AGAIN AGAIN: \(TestimonialData.companyNames)")
}
//Loses Scope
print("Companies AGIAN TIMES : \(TestimonialData.companyNames)")
task.resume()
print("Outside of loop Customers Number: \(TestimonialData.customerNames.count)")
print("Outside of loop Testimonial Text Number: \(TestimonialData.testimonialsText.count)")
print(TestimonialData.companyNames)
}
I know I'm missing something really simple...but I'm at a loss... Any help/info is appreciated!

There are a few issues with this code:
First: The JSON you are receiving is not in the format your code expects. The root object isn't a dictionary but an array.
if let JSONfile = json as? [String: Any] {
changes to
if let JSONfile = json as? [[String: String]] {
This also requires you to loop over each item.
print("JSON Reading")
for item in JSONfile {
As the dictionary definition has changed from [String:Any] to [String:String] the as? String statements are no longer needed either.
Second: I'm not sure if you realise this or not (maybe you already do) but the bits of code after the //Loses Scope line run first. Before the Companies AGIAN AGAIN and Companies AGIAN AGAIN AGAIN lines.
They might be further down the page but the lines above that are in a closure that runs after the file has downloaded and so execute later after the other lines have already run.
Here is the complete fixed code (formatted in such a way you can copy and paste it into an Xcode Playground to see it working).
// Xcode 8.2, Swift 3
import Cocoa
import PlaygroundSupport
// Required for the download to work.
PlaygroundPage.current.needsIndefiniteExecution = true
struct GlobalTestimonialData {
var testimonialsText: [String]
var customerNames: [String]
var companyNames: [String]
}
var testimonialData = GlobalTestimonialData(testimonialsText: [], customerNames: [], companyNames: [])
func getData () {
let requestURL: NSURL = NSURL(string: "https://szadydesigns.com/test/mobileapp/testimonials.php")!
let urlRequest: NSMutableURLRequest = NSMutableURLRequest(url: requestURL as URL)
let session = URLSession.shared
let task = session.dataTask(with: urlRequest as URLRequest) { (data, response, error) in
let httpResponse = response as! HTTPURLResponse
let statusCode = httpResponse.statusCode
if (statusCode == 200) {
print("File has been downloaded!")
do {
let json = try JSONSerialization.jsonObject(with: data!, options:.allowFragments)
print("JSON Serialized")
if let JSONfile = json as? [[String: String]] {
print("JSON Reading")
for item in JSONfile {
if let testimonial = item["testimonial"] {
testimonialData.testimonialsText.append(testimonial)
if let name = item["name"] {
testimonialData.customerNames.append(name)
}
if let company = item["company"] {
testimonialData.companyNames.append(company)
}
}
}
}
} catch {
print("Error with Json: \(error)")
}
}
print("Companies Last: \(testimonialData.companyNames)")
print(" ")
print(testimonialData)
}
//Loses Scope
print("Companies 1 : \(testimonialData.companyNames)")
task.resume()
print("Before the download of the JSON Customer names count: \(testimonialData.customerNames.count)")
print("Before the download of the JSON Testimonial Text Number: \(testimonialData.testimonialsText.count)")
print(testimonialData.companyNames)
}
getData()
And you will get this output, which helps explain what is going on (though I removed the actual company names).
Companies 1 : []
Before the download of the JSON Customer names count: 0
Before the download of the JSON Testimonial Text Number: 0
[]
File has been downloaded!
JSON Serialized
JSON Reading
Companies: ["REMOVED_1", "REMOVED_2", "REMOVED_3", "REMOVED_4"]

Related

How to parse JSON using init()

I can't display the json Array by using its object
showing this error :
"Thread 1: Fatal error: Unexpectedly found nil while unwrapping an
Optional value"
class sample{
var jarray:[[String:Any]]!
init(url: String) {
let urll = URL(string: url)
var request = URLRequest(url: urll!)
request.httpMethod = "GET"
request.addValue("application/json", forHTTPHeaderField: "Content-Type")
let task = URLSession.shared.dataTask(with: request, completionHandler: {(Data,response,Error) in
do
{
let jsonresponse = try JSONSerialization.jsonObject(with: Data!, options: [])
let jsonarray = jsonresponse as? [[String:Any]]
self.jarray = jsonarray!
print(self.jarray)
DispatchQueue.main.async {
}
}
catch let parsingerror
{
print("error",parsingerror)
}
})
task.resume()
}
}
First of all: Handle always errors and unwrap optionals safely.
Second of all Data and Error (capitalized) are reserved words, use always lowercased parameter labels in closures (and uppercased class names).
Many lines in your code are redundant.
class Sample {
var jarray = [[String:Any]]()
init(url: String) {
guard let urll = URL(string: url) else { return }
let task = URLSession.shared.dataTask(with: urll) { data, _ , error in
if let error = error { print(error); return }
do
{
// if error is nil then data is guaranteed to be non-nil
if let jsonarray = try JSONSerialization.jsonObject(with: data!) as? [[String:Any]] {
self.jarray = jsonarray
print(self.jarray)
DispatchQueue.main.async {
}
}
}
catch {
print("error", error)
}
}
task.resume()
}
}
Note: It's bad practice to run asynchronous tasks in init methods
Avoid using force unwrapping unnecessarily. I might result in unwanted crashes in your app. In your code,
Check if Data is nil. If it is, the below line will result in runtime exception.
let jsonresponse = try JSONSerialization.jsonObject(with: Data!, options: [])
In the below line of code, check whether jsonarray is nil.
self.jarray = jsonarray!
If not, then add the line where your app is crashing.
Try replacing your code with:
class sample {
var jarray: [[String:Any]]?
init(url: String) {
if let urll = URL(string: url) {
URLSession.shared.dataTask(with: urll) { (data, response, error) in
do {
if let data = data {
let jsonresponse = try JSONSerialization.jsonObject(with: data, options: [])
self.jarray = jsonresponse as? [[String:Any]]
print(self.jarray)
DispatchQueue.main.async {
}
}
} catch {
print("error",error)
}
}.resume()
}
}
}
Also, don't use reserved words as variable names like you did for Data and Error.
Most importantly - Never use forced unwrapping (!) with server response. API response might not be as expected always. Try handling that.
JSONSerialization is now old-fashioned way. Apple introduced Codable protocol that handles for you serialisation and deserialisation of objects.
Example:
struct Photo: Codable
{
//String, URL, Bool and Date conform to Codable.
var title: String
var url: URL
var isSample: Bool
//The Dictionary is of type [String:String] and String already conforms to Codable.
var metaData: [String:String]
//PhotoType and Size are also Codable types
var type: PhotoType
var size: Size
}
And in the response from the server:
if let jsonData = jsonString.data(using: .utf8)
{
let photoObject = try? JSONDecoder().decode(Photo.self, from: jsonData)
}

problem in Parsing json in SWIFT for iOS developing

I have an Error in parsing JSON Format form an ASMX Web service,
My Code is
func getData() {
let url = URL(string: "http://192.168.11.188/getItems.asmx/theItems")
let theCategory = "ALL"
let theSubCategory = "ALL"
let postString = "theCategory=\(theCategory)&theSubCategory=\(theSubCategory)"
var request = URLRequest(url: url!)
request.httpMethod = "POST"
request.cachePolicy = NSURLRequest.CachePolicy.reloadIgnoringCacheData
request.httpBody = postString.data(using: String.Encoding.utf8)
URLSession.shared.dataTask(with: request) { (data, respons, error) in
if let error = error {
print("Error conning to server \(error)")
} else {
if let respons = respons as? HTTPURLResponse {
if respons.statusCode == 200 {
print(data!)
if let data = data {
do {
let json = try JSONDecoder().decode([ITEMS].self, from: data)
print(json)
} catch let parsingError {
print("Error parsing json \(parsingError)")
}
}
} else {
print("Error in responce code.... \(respons.statusCode)")
}
}
}
}.resume()
}
I am using the decoder struct in this code:
struct ITEMS: Codable {
let CODE:String
let CAT_ID:String
let SUB_ID:String
let PRODUCT_AR:String
let PRODUCT_EN:String
let OLD_PRICE:String
let NEW_PRICE:String
let UNIT:String
let BARCODE:String
let THE_DATE:String
let TIME:String
}
The JSON value is
{ ITEMS : [{"CODE":111,"CAT_ID":203,"SUB_ID":null,"PRODUCT_AR":"ITEM 1","PRODUCT_EN":"ITEM 1","OLD_PRICE":133.0035,"NEW_PRICE":109,"UNIT":null,"BARCODE":"328031002009","THE_DATE":"\/Date(1553673958397)\/","TIME":"11:05 AM"},
{"CODE":222,"CAT_ID":201,"SUB_ID":null,"PRODUCT_AR":"ITEM 2","PRODUCT_EN":"ITEM 2","OLD_PRICE":18.95,"NEW_PRICE":9.95,"UNIT":null,"BARCODE":"628103400012","THE_DATE":"\/Date(1553673958260)\/","TIME":"11:05 AM"}]}
but this code returns an Error
Error parsing JSON Error Domain=NSCocoaErrorDomain Code=3840 "JSON text did not start with array or object and option to allow fragments not set." UserInfo={NSDebugDescription=JSON text did not start with array or object and option to allow fragments not set.}
What am I doing wrong?
as you are writing this,
let json = try JSONDecoder().decode([ITEMS].self, from: data)
it must ask for array of ITEMS to be parsed, but your JSON is just an object not an array (It starts with {ITEMS: [array_here]}).
For solving this issue, you need to make your struct like below and parse data into that object.
struct ITEMS: Codable {
let CODE:String
let CAT_ID:String
let SUB_ID:String
let PRODUCT_AR:String
let PRODUCT_EN:String
let OLD_PRICE:String
let NEW_PRICE:String
let UNIT:String
let BARCODE:String
let THE_DATE:String
let TIME:String
}
struct MyAPIData: Codable {
let ITEMS: [ITEMS]
}
Now parse data using below line of code,
let json = try JSONDecoder().decode(MyAPIData.self, from: data)
There were wrong types used in your struct, here is the fixed struct
struct ITEM: Codable {
let CODE:Int // not String
let CAT_ID:Int // not String
let SUB_ID:Int? // its null in JSON, use either Int? or String?
let PRODUCT_AR: String
let PRODUCT_EN: String
let OLD_PRICE:Double // not String
let NEW_PRICE:Double // not String
let UNIT:String?
let BARCODE:String
let THE_DATE:String
let TIME:String
}
Since you JSON has a root element ITEMS, you need to decode using this struct
struct BaseItems: Codable {
let ITEMS: [ITEM] // Actual array of items are within this JSON element
}
Usage:
do {
let decoded = try JSONDecoder().decode(BaseItems.self, from: data)
print(decoded.ITEMS)
} catch {
print(error)
}

How to parse a api for swift 3?

Have been researching on the parsing for quite a bit. With plethora of information avilable for JSON nothing seems to explain how to do in a sensible way to extract information with swift 3.
This is what got so far
func getBookDetails() {
let scriptUrl = "https://www.googleapis.com/books/v1/volumes?q=isbn:9781451648546" .
let myurl = URL(string:scriptUrl)
let request = NSMutableURLRequest(url: myurl!)
request.httpMethod = "GET"
let task = URLSession.shared.dataTask(with: myurl! ) { (data, response, error) in
if error != nil{
print("THIS ERROR",error!)
return
} else{
if let mydata = data{
do{
let myJson = try (JSONSerialization.jsonObject(with: mydata, options: JSONSerialization.ReadingOptions.mutableContainers)) as AnyObject
// print("this is the MY JSON",myJson) ---> prints out the json
if let dictonary = myJson["items"] as AnyObject? {
print("the DICTONARY",dictonary) // ----> OUTPUT
if let dictonaryAA = dictonary["accessInfo"] as AnyObject? {
print("the accessInfo",dictonaryAA)
}
}
} catch{
print("this is the in CATCH")
}
} //data
}
}
task.resume()
}
}
OUTPUT :
the DICTONARY (
{
accessInfo = {
accessViewStatus = SAMPLE;
country = US;
=============
RELEVANT DATA as in https://www.googleapis.com/books/v1/volumes?
q=isbn:9781451648546"
==========================
title = "Steve Jobs";
};
}
)
Just need to parse through the json data to get the name, author and title of the book with reference to isbn.
Know there should be a better way to do things that is easily understandable to someone new into the language
You can parse the api in two ways
Using URLSession:
let rawDataStr: NSString = "data={\"mobile\":\"9420....6\",\"password\":\"56147180..1\",\"page_no\":\"1\"}"
self.parsePostAPIWithParam(apiName: "get_posts", paramStr: rawDataStr){ ResDictionary in
// let statusVal = ResDictionary["status"] as? String
self.postsDict = (ResDictionary["posts"] as! NSArray!) as! [Any]
print("\n posts count:",self.postsDict.count)
}
func parsePostAPIWithParam(apiName:NSString, paramStr:NSString,callback: #escaping ((NSDictionary) -> ())) {
var convertedJsonDictResponse:NSDictionary!
let dataStr: NSString = paramStr
let postData = NSMutableData(data: dataStr.data(using: String.Encoding.utf8.rawValue)!)
let request = NSMutableURLRequest(url: NSURL(string: "http://13.12..205.248/get_posts/")! as URL,
cachePolicy: .useProtocolCachePolicy,
timeoutInterval: 10.0)
request.httpMethod = "POST"
request.allHTTPHeaderFields = nil
request.httpBody = postData as Data
let session = URLSession.shared
let dataTask = session.dataTask(with: request as URLRequest, completionHandler: { (data, response, error) -> Void in
if (error != nil) {
print(error as Any)
} else {
let httpResponse = response as? HTTPURLResponse
print(httpResponse as Any)
do{
if let convertedJsonIntoDict = try JSONSerialization.jsonObject(with: data!, options: []) as? NSDictionary {
convertedJsonDictResponse = convertedJsonIntoDict.object(forKey: apiName) as? NSDictionary
// callback for response
callback(convertedJsonDictResponse)
}
} catch let error as NSError {
print(error)
}
}
Using Alamofire
func AlamofirePOSTRequest() {
let urlString = "http://13.12..205.../get_posts/"
let para = ["data": "{\"mobile\":\"9420....6\",\"password\":\"56147180..1\",\"page_no\":\"1\"}"]
Alamofire.request(urlString, method: .post, parameters: para , headers: nil).responseJSON {
response in
switch response.result {
case .success:
print("response: ",response)
let swiftyJsonVar = JSON(response.result.value!)
if let resData = swiftyJsonVar["posts"].arrayObject {
self.postsDict = resData as! [[String:AnyObject]]
}
print("\n \n alomafire swiftyJsonVar: ",swiftyJsonVar)
break
case .failure(let error):
print(error)
}
}
}
})
dataTask.resume()
}
First of all, all JSON types are value types in Swift 3 so the most unspecified type is Any, not AnyObject.
Second of all, there are only two collection types in the JSON type set, dictionary ([String:Any]) and array ([Any], but in most cases [[String:Any]]). It's never just Any nor AnyObject.
Third of all, the given JSON does not contain a key name.
For convenience let's use a type alias for a JSON dictionary:
typealias JSONDictionary = [String:Any]
The root object is a dictionary, in the dictionary there is an array of dictionaries for key items. And pass no options, .mutableContainers is nonsense in Swift.
guard let myJson = try JSONSerialization.jsonObject(with: mydata) as? JSONDictionary,
let items = myJson["items"] as? [JSONDictionary] else { return }
Iterate through the array and extract the values for title and authors which is an array by the way. Both values are in another dictionary for key volumeInfo.
for item in items {
if let volumeInfo = item["volumeInfo"] as? JSONDictionary {
let title = volumeInfo["title"] as? String
let authors = volumeInfo["authors"] as? [String]
print(title ?? "no title", authors ?? "no authors")
The ISBN information is in an array for key industryIdentifiers
if let industryIdentifiers = volumeInfo["industryIdentifiers"] as? [JSONDictionary] {
for identifier in industryIdentifiers {
let type = identifier["type"] as! String
let isbn = identifier["identifier"] as! String
print(type, isbn)
}
}
}
}
You are doing wrong in this line
if let dictonaryAA = dictonary["accessInfo"] as AnyObject?
because dictonary here is an array not dictionary. It is array of dictionaries. So as to get first object from that array first use dictonary[0], then use accessInfo key from this.
I am attaching the code for your do block
do{
let myJson = try (JSONSerialization.jsonObject(with: mydata, options: JSONSerialization.ReadingOptions.mutableContainers)) as AnyObject
// print("this is the MY JSON",myJson) ---> prints out the json
if let array = myJson["items"] as AnyObject? {
print("the array",array) // ----> OUTPUT
let dict = array.object(at: 0) as AnyObject//Master Json
let accessInf = dict.object(forKey: "accessInfo") //Your access info json
print("the accessInfo",accessInf)
}
}
Hope this helps you.

Swift - Troubles with API calls / CompletionHandler / URLSessions

I have this long function here which makes a bunch of API calls, parses through data and returns two arrays representing a bunch of sight-seeing locations (one array holds the latitudes, one holds the latitudes). The issue I am having is determining when the two arrays are finished being populated. Ideally, I would like to be able to place
print("ArrayCount = \(self.latArray.count)")
somewhere in my code and receive a single print statement in the console reading ArrayCount = 123. However everywhere I place the print statement I get either an array count of 0 or a loop of values being printed out as they are added (1..2..3.. ... ..123). Thanks in advance!
func someFunction()
{
let url:URL = URL(string: "...")
let task = URLSession.shared.dataTask(with: URLRequest(url: url))
{
data, response, error in
if error != nil
{
print("ERROR IN API REQUEST: \(error!.localizedDescription)")
}
else
{
do
{
if let parsedData = try JSONSerialization.jsonObject(with: data!, options: .allowFragments) as? [String: Any]
{
if let layerOne = parsedData["one"] as? [String: Any]
{
if let layerTwo = layerOne["two"] as? [[String: Any]]
{
for layerThree in layerTwo
{
if let variableName = layerThree["value"] as? String
{
let innerUrl:URL = URL(string: "...")!
let innerTask = URLSession.shared.dataTask(with: URLRequest(url: innerUrl))
{
data, response, error in
if error != nil
{
print("ERROR IN API REQUEST: \(error!.localizedDescription)")
}
else
{
do
{
if let parsedData = try JSONSerialization.jsonObject(with: data!, options: .allowFragments) as? [String: Any]
{
if let layerA = parsedData["A"] as? [String: Any]
{
if let lat = layerA["Latitude"] as? String, let long = layerA["Longitude"] as? String
{
self.latArray.append(lat)
self.longArray.append(lon)
}
}
}
}
catch
{
print("ERROR IN JSON SERIALIZATION")
}
}
}
innerTask.resume()
}
}
}
}
}
}
catch
{
print("ERROR IN JSON SERIALIZATION")
}
}
}
task.resume()
}
I'm not sure exactly what you're trying to do, but once I thinned out your incredibly verbose code and tried to understand it, I think the conclusion is that you'll only want to record the array counts after the last iteration of your inner loop. For example:
Instead of doing this: for layerThree in layerTwo
do this: for (index, layerThree) in layerTwo.enumerated()
Then, after you append the lat/long values, add this check:
if index == layerTwo.count - 1 //if this is our last inner loop
{
//print the array counts
print("Lat count: \(latArray.count)")
print("Long count: \(longArray.count)")
}
This should work in your case. However, I disagree entirely with the execution of this simply because of the lack of portability and reusability of your code. Additionally, there are quite a few good language constructs which are being ignored. The relentless nested if let blocks could be reduced significantly with a few good guard statements. Furthermore, considering you aren't handling your errors in any catch blocks anyway, might as well just remove them and opt for a try? instead. One much swiftier way of handling things would be to just include a completion handler in the function itself so that the array count printing logic could be handled elsewhere. I'll include some example code of how I would clean things up:
func someFunction(completion: (([String], [String]) -> Void)?)
{
let url = URL(string: "...")!
var latArray: [String] = []
var longArray: [String] = []
let task = URLSession.shared.dataTask(with: URLRequest(url: url),
completionHandler: { (data, response, error) -> Void in
guard error == nil else
{
print("ERROR IN API REQUEST: \(error?.localizedDescription)")
return
}
guard let parsedData = try? JSONSerialization.jsonObject(with: data!, options: .allowFragments),
let parsedDict = parsedData as? [String: Any],
let layerOne = parsedDict["one"] as? [String: Any],
let layerTwo = layerOne["two"] as? [[String: Any]] else
{
print("JSON OBJECT DOES NOT MATCH EXPECTED PATTERN")
return
}
for (index, layerThree) in layerTwo.enumerated()
{
guard let _ = layerThree["value"] as? String else
{
print("Skip this iteration (I guess?)")
continue
}
let innerUrl = URL(string: "...")!
let innerTask = URLSession.shared.dataTask(with: URLRequest(url: innerUrl),
completionHandler: { (data, response, error) -> Void in
guard error == nil else
{
print("ERROR IN API REQUEST: \(error?.localizedDescription)")
return
}
guard let parsedData = try? JSONSerialization.jsonObject(with: data!, options: .allowFragments),
let parsedDict = parsedData as? [String: Any],
let layerA = parsedDict["A"] as? [String: Any],
let lat = layerA["Latitude"] as? String,
let long = layerA["Longitude"] as? String else
{
print("Something went wrong")
return
}
latArray.append(lat)
longArray.append(long)
if index == layerTwo.count - 1 //if this is our last inner loop
{
completion?(latArray, longArray)
}
}
)
innerTask.resume()
}
}
)
task.resume()
}
someFunction(completion: { (latArray, longArray) -> Void in
//print the array counts
print("Lat count: \(latArray.count)")
print("Long count: \(longArray.count)")
})
This could still be improved much further, though based on your question I don't see that I personally should be putting more time into constructing this for you. It would be best if you come up with a few methods of your own to build an application with more readable and reusable code.

Downloading JSON and Initializing Struct with Data [duplicate]

I'm having a problem with the following code. I'm downloading a list of actors in JSON and I want to populate Struct Actor with the received data. Everything works great until I try to flatMap on the received data and try to initialize the struct Actor. When I try to compile the code i get the error: Cannot assign value of type '()' to type [Actor]. The error corresponds to a line in viewDidLoad actorsList = downloadActors() Would anybody have any recommendation who to solve this?
import UIKit
func downloadActors() {
var request = URLRequest(url: URL(string: "url...")!)
request.httpMethod = "POST"
let postString = "actorGroup=\("Superhero")"
request.httpBody = postString.data(using: .utf8)
let task = URLSession.shared.dataTask(with: request) { data, response, error in
DispatchQueue.main.async {
guard let data = data, error == nil else {
print("error=\(error)")
return
}
if let httpStatus = response as? HTTPURLResponse, httpStatus.statusCode != 200 {
print("error : statusCode should be 200 but is \(httpStatus.statusCode)")
print("response = \(response)")
}
if let httpStatus = response as? HTTPURLResponse, httpStatus.statusCode == 200 {
do {
let json = try JSONSerialization.jsonObject(with: data, options: JSONSerialization.ReadingOptions.mutableContainers) as? [String: AnyObject]
guard let actorsJSON = json?["response"] as? [[String : AnyObject]] else {
return
}
} catch {
print("catch error")
}
}
}
}
task.resume()
}
func loadActors() -> [Actor] {
if let actors = actorsJSON as? [[String : AnyObject]] {
return actors.flatMap(Actor.init)
}
}
let actorsArray = loadActors()
class MasterViewController: UITableViewController {
var actorsList = [Actor]()
var detailViewController: DetailViewController? = nil
var objects = [Any]()
override func viewDidLoad() {
super.viewDidLoad()
actorsList = downloadActors()
print(actorsList)
Struct Actors is as follows:
struct Job {
let actorGroup: String
let actorName: String
}
extension Actor: JSONDecodable {
init?(JSON: [String : AnyObject]) {
guard let actorGroup = JSON["actorGroup"] as? String, let actorName = JSON["actorName"] as? String else {
return nil
}
self. actorGroup = actorGroup
self. actorName = actorName
}
}
let listActors = actorsJSON as? [[String : AnyObject]] {
Should be:
if let listActors = actorsJSON as? [[String : AnyObject]] {
Edit: For more info I'd like to add Vadian's comment:
Very confusing code. What does the function in the middle of the do block? Why do you type-check actorsJSON twice? The computed property is let listActors... which should be probably an optional binding (if let ... ). Further .mutableContainers is completely nonsense in Swift. And finally a JSON dictionary is [String:Any] in Swift 3.

Resources