1

I have a variable named currentYear that is set to 2000 for default purposes. currentYear is a variable in my array of strings so the string knows which year it is. This is in a file which has no class. These values are globally accessible.

import UIKit

var currentYear = 2000

var array = [
"The year you pressed is \(currentYear)",

]

In the ViewController I have buttons with tags that are equal to the year it contains: 2013, 2014, 2015 and 2016. When the button is pressed, it goes to another ViewController.

import UIKit

class ViewController: UIViewController {

    @IBAction chooseYearBtnPressed(sender: AnyObject) { 

        currentYear = sender.tag 

        print(array)   

        performSegueWithIdentifier("CustomSegue", sender: nil)

    }


}

Before the recent xcode update, this worked just fine. Whenever the button was pressed, the array that is loaded has the same currentYear as whatever was the new value. Now it doesn't. After assigning currentYear a new value, the currentYear in the array remains 2000.

How can I fix this, so currentYear would still change based on whatever I pass as a new value?

Solution:

var array: [String] { 
    return ["The Year you pressed is \(currentYear)"] 
}
2
  • I didn't track Swift evolution for a while but maybe you can find something within the release notes of the current Xcode version. Usually Apple lists all changes made to Swift when releasing a new Xcode. Commented Mar 23, 2016 at 11:15
  • I did. Can't find the reason in there though, or would I know how to fix this. Commented Mar 23, 2016 at 11:20

2 Answers 2

1

I don't see how your code ever did what you want it to do. Your code that assigns a value to array fires once when you first create an instance of your view controller, and not again. (I'm assuming that your definitions of currentYear and array are inside a view controller so they are properties of the view controller.)

If you want to update array each time the value of currentYear changes, you should add a didSet function to currentYear. Something like this:

import UIKit

var array: [String]
var currentYear
{
  didSet 
  {
    array = 
    [
      "The year you pressed is \(currentYear)",
    ]
  }
}
currentYear = 2000
Sign up to request clarification or add additional context in comments.

2 Comments

They are not in a viewController or any class. They are in a separate file which has no class which is/can be accessed by any other viewcontroller. And it did work before, otherwise I wouldn't post this 😋 To be clear, currentYears DOES change. But the Array doesn't. I will try your answer now though
Your solution works just fine! I only needed to use a getter though. Not a didSet.
1

Try to clear the current variable and array before assigning the tag value for it , becoz in my opinion it is overriding somewhere ..Iam not sure though just give a try..:-)

3 Comments

That it is overriding somewhere was true. I changed currentYear to { var currentYear: Int! } and that worked. I don't know why this worked, but it worked nonetheless. Nor do I know why { var currentYear = 2000 } worked before and now doesn't. Either way, you helped. Thanks!
I still had problems later on in the code. After trying Duncans solution, it worked just fine.
your code worked for me. it should be the selected answer +1

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.