I'm working on an app in Swift that makes a request to a server pulls back JSON and parses it into a schedule. I have table view controller with 7 cells and a label in each. I am aiming to change the text of each label after the request.
@IBOutlet weak var firstPeriod: UILabel!
@IBOutlet weak var secondPeriod: UILabel!
@IBOutlet weak var thirdPeriod: UILabel!
@IBOutlet weak var fourthPeriod: UILabel!
@IBOutlet weak var fifthPeriod: UILabel!
@IBOutlet weak var sixthPeriod: UILabel!
@IBOutlet weak var seventhPeriod: UILabel!
override func viewDidLoad() {
super.viewDidLoad()
let dayUrl = NSURL(string: "http://localhost:3000/day")
func setSchedLayout() {
var firstPeriodClass = String(defaults.dictionaryForKey("todaySchedule")!["first"]!["name"]!)
self.firstPeriod.text = firstPeriodClass
print(firstPeriodClass)
}
let task = NSURLSession.sharedSession().dataTaskWithURL(dayUrl!) {(data, response, error) in
day = NSString(data: data!, encoding: NSUTF8StringEncoding)!.stringByReplacingOccurrencesOfString("\"", withString: "").stringByReplacingOccurrencesOfString(" ", withString: "").stringByReplacingOccurrencesOfString("D", withString: "d")
var todaySchedule = defaults.dictionaryForKey("FullWeekSchedule")![day]
defaults.setObject(todaySchedule, forKey: "todaySchedule")
setSchedLayout()
}
task.resume()
}
This all happens inside my viewDidLoad()
I already have some data in my UserDefaults.
When my setSchedLayout()
function calls at the end of my request, it doesn't change the text value for about 2 minutes. Then it gives me an error that says I'm trying to
modify the autolayout engine from a background thread
And when it does change the text it gives it an optional string. What's going on?