NSUserDefaults messing with SegmentedControl
<p>I have a simple expense tracker app that has at the top a Segmented control to change currency (Romanian currency, Euro and $ in this order). In my list manager I have methods that convert from one to another and in the view controller, based on the Segmented control, I call them accordingly.</p>

<p>My problem is the following: When the app starts, the selected currency is always the first - Ro currency. If I add some elements and then I quit the app with another currency selected, I guess NSUserDefaults synchronizes with those values. When I open the app again, Ro currency is again selected, but with the $/euro values. (Say I add 410 ron, which converted is ~$100. I select $ and kill the app from multitasking. When I open it up again, it show 100 ron, instead of 410). If a currency is selected and add a certain amount, it performs ok (if $ is selected and I add 100, when I switch the control to ron it displays 410). I guess I have to change something with the synchronization, but I can't figure out where and how.</p>

<p>EDIT2: Some code  (sorry)</p>

<p>//This is the expenses class
import UIKit</p>

<p>class Expense: NSObject, NSCoding {</p>

<pre><code>//MARK: Properties

var title:String?
var amount:Double?

init(title:String,amount:Double){
    self.title = title
    self.amount = amount
}

override init(){
    super.init()
}

required init(coder aDecoder: NSCoder){
    if let titleDecoded = aDecoder.decodeObjectForKey("title") as? String{
        title = titleDecoded
    }
    if let amountDecoded = aDecoder.decodeObjectForKey("amount") as? Double{
        amount = amountDecoded
    }

}

func encodeWithCoder(aCoder: NSCoder) {
    if let titleEncoded = title {
        aCoder.encodeObject(titleEncoded, forKey:"title")
    }
    if let amountEncoded = amount {
        aCoder.encodeObject(amountEncoded, forKey:"amount")
    }
}
</code></pre>

<p>}</p>

<p>//The NSUserDefaultsManager 
import UIKit</p>

<p>class NSUserDefaultsManager: NSObject {
    static let userDefaults = NSUserDefaults.standardUserDefaults()</p>

<pre><code>class func synchronize(){
    let myData = NSKeyedArchiver.archivedDataWithRootObject(ExpenseManager.expenses)

    NSUserDefaults.standardUserDefaults().setObject(myData, forKey: "expensearray")
    NSUserDefaults.standardUserDefaults().synchronize()
}

class func initializeDefaults(){
    if let expensesRaw = NSUserDefaults.standardUserDefaults().dataForKey("expensearray"){
        if let expenses = NSKeyedUnarchiver.unarchiveObjectWithData(expensesRaw) as? [Expense]{
            ExpenseManager.expenses = expenses
        }
    }
}
</code></pre>

<p>}</p>

<p>I call the initializeDefaults in the tableview manager in view did load, the synchronize in view did appear and in the App Delegate module, sync in applicationWillTerminate.</p>

<p>ANSWER</p>

<p>I found a solution - it was quite obvious. I found it in a Google Books - iOS 9 Programming Fundamentals by Matt Neuburg. 
In the segmented control action I added the following</p>

<p>let c = self.currencySelector.selectedSegmentIndex
            NSUserDefaults.standardUserDefaults().setObject(c, forKey: "selectedcurrency")</p>

<p>while in viewDidLoad I added</p>

<p>let c = NSUserDefaults.standardUserDefaults().objectForKey("selectedcurrency") as! Int
        currencySelector.selectedSegmentIndex = c</p>