0

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?

awph
  • 566
  • 6
  • 15
Isaac Wasserman
  • 1,461
  • 4
  • 19
  • 39
  • 4
    **NEVER** update UI from a background thread! You need to dispatch the update in the main thread. Try `dispatch_async(dispatch_get_main_queue(), { setSchedLayout() })` – awph Nov 11 '15 at 18:40
  • Possible duplicate of [Getting a "This application is modifying the autolayout engine" error?](http://stackoverflow.com/questions/28302019/getting-a-this-application-is-modifying-the-autolayout-engine-error) – awph Nov 11 '15 at 19:29
  • @awph thank you so much that worked. However, the value is still optional. When I try to unwrap it with `!` it gives me an error saying that it is non-optional. – Isaac Wasserman Nov 11 '15 at 23:14
  • Where? for `todaySchedule` or `firstPeriodClass`? Maybe try to synchronize your user defaults (`defaults.synchronize()`). – awph Nov 11 '15 at 23:18
  • Never mind @awph I figured it out. The "Optional(" + ")" had been baked into the string – Isaac Wasserman Nov 11 '15 at 23:33
  • Ok by the way even one or the other, the optional value here is because if you call the `dictionaryForKey` and no dictionary exists for the key, it'll return `nil`. If you are sure that the key exist, use `!` otherwise create a new one with the *nil coalescing* operator for example. – awph Nov 11 '15 at 23:41

0 Answers0