1

What I'm trying to do is to get today's weekday. From it I get the rest 6 weekdays, I put them into an array and then try to use this array to update 7 UILabels. A short list of weekdays starting from today that is. My UILabels are in a referencing outlet collection and I have set their tags from 0 to 6.

I have tried multiple different versions (originally without any array) of this and have managed to make this work on Playground, up to the point that I have the weekdays in the array but on the actual project, when I try to update the labels, it builds successfully but then crashes.

Edit: Crash comes after successful build. Error in Thread 1: breakpoint 1.1. Stops at the row super.viewDidLoad() apparently.

Edit2: made changes as suggested. Build succeeds and then crashes in the same style as in first Edit.

Any help is appreciated.

import UIKit

class ViewController: UIViewController {
    @IBOutlet var label: [UILabel]!

    var rollingDay = NSDate()
    var array: [String]  = []

    func addDays() {
        var calendar = NSCalendar.currentCalendar()
        var formatter = NSDateFormatter()
        var stringRolling = formatter.stringFromDate(rollingDay)

        for var x = 0; x<7; x++ {
            calendar = NSCalendar.currentCalendar()
            formatter = NSDateFormatter()
            formatter.dateFormat = "EEEE"
            stringRolling = formatter.stringFromDate(rollingDay)
            array.append(stringRolling)
            rollingDay = calendar.dateByAddingUnit(.CalendarUnitDay, value: 1, toDate: rollingDay, options: nil)!
        }
    }
func updateLabels() {
    for (index, day) in enumerate(array) {
        let label : UILabel = self.view.viewWithTag(index) as! UILabel
        label.text = day as String
    }
}

override func viewDidLoad() {
    super.viewDidLoad()

    addDays()
    updateLabels()

        // Do any additional setup after loading the view, typically from a nib.
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
}
1
  • You need to update your question with details about the crash. Commented Sep 16, 2015 at 15:25

1 Answer 1

2

Since you have already used array to keep all the weekdays, now you just have to enumerate the array to set the labels as shown below:

for (index, day) in enumerate(array) {

    let label : UILabel = self.view.viewWithTag(index) as! UILabel
    label.text = day as String
}
Sign up to request clarification or add additional context in comments.

6 Comments

Can you show your solution in the original code? My knowledge of Swift is very limited, I tried to integrate your solution with variable interesting errors. In my solution I append the weekdays to the array, using NSArray this doesn't seem to work. Thanks in advance.
Made the changes to the original code. Builds ok but crashes as in first Edit. Puzzling.
what are the elements in your array? on what does it crash and with what exception reason? please post tht here.
Array consist of weekdays that are provided by NSDate. Now I managed to get an actual error report, it says "Could not cast value of type 'UIView' (0x1996ff510) to 'UILabel' (0x199700e10)."
This is happening because there must be a UIView with tag 0 (because 0 is default tag value) in the subviews of self.view. That is why self.view.viewWithTag(0) will crash as it is not a UILabel. You cannot use 0 tag for any weekday. Try to add a constant number to all the weekday indices.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.