Hi
Similar to my other question, I’m working at building essentially a web service to display data from my own JSON API. I feel that is a decent complexity to start of with learning swift and the iOS development. I’ve managed to get my json parser to get and read the data. Thanks to @narrativium for his help with checking over my stuff. Everything has been done through the viewDidLoad() method. I’ve been working my way through a Udemy course, which is good in principle, but coming from a programming background I feel it isn’t great separation of code? And the parser should become more flexible. I also used a tutorial here https://www.raywenderlich.com/113388/storyboards-tutorial-in-ios-9-part-1 on storyboards which was great. And I’m trying to change the Player.swift (Mine is called Game.swift) and PlayerData.swift (Mine is called SampleData) files from that tutorial to collected data from the json API. I’m still fumbling my way through iOS and Swift so wanted some guidance. This is how I’ve started to rearrange my code now:
I have:
GameController.swift (UIViewController + UITableViewDelegate) that lists some current game scores by pulling from my API
NewsController.swift (UIViewController + UITableViewDelegate) that lists the latest stories from my API
SampleData.swift from ray wenderlich tutorial
class Data: UIViewController {
let GameData = [
Game(timer: 30, home: "Man City", away: "Chelsea", score: "2 - 2", homeBadge: "city.png", awayBadge: "chelsea.png"),
Game(timer: 80, home: "Liverpool", away: "Man U", score: "1 - 2", homeBadge: "liverpool.png", awayBadge: "manu.png"),
]
}
** Game **
import UIKit
struct Game {
var timer: Int?
var home: String
var away: String
var score: String
var homeBadge: String
var awayBadge: String
init(timer: Int?, home: String, away: String, score: String, homeBadge: String, awayBadge: String) {
self.timer = timer
self.home = home
self.away = away
self.score = score
self.awayBadge = awayBadge
self.homeBadge = homeBadge
}
}
Api.swift my model for pulling the data.
import Foundation
class Api {
// api url string
let url = NSURL(string: "http://api.dev/web/game-view")!
func DataManager() -> [Dictionary<String, String>] {
// start task manager
let task = NSURLSession.sharedSession().dataTaskWithURL(url) { (data, response, error) in
// init array of dictionaries
var gameData = [Dictionary<String, String>]()
// gracefully continue if no data
guard let urlContent = data
else { return }
do {
// create json object
let jsonObject = try NSJSONSerialization.JSONObjectWithData(urlContent, options: .MutableContainers)
// down cast types
guard let jsonArray = jsonObject as? [[String: AnyObject]]
else { return }
// loop through jsonArray and extract values where possible
for dictionary in jsonArray {
// if home & away scores exist a "score" can be created
if let home_score = dictionary["home_score"] as? Int,
away_score = dictionary["away_score"] as? Int {
let score = ["score": "\(home_score) - \(away_score)"]
gameData.append(score)
return gameData // Error: unexpected non-void return value in void function
}
}
} catch {
print("Error")
}
}
// resume task regardless
task.resume()
}
}
I’m not quite sure of a couple things here:
1 - What’s the way to return data from my DataManager() method in the Api.swift? I’m assuming the error is because it’s inside the completion handler, but don’t know how to accomplish that.
2 - In terms of design implementation how should I be going about converting the code from tutorial, do I need to keep my Game.swift struct?