tudors5
tudors5

Reputation: 41

NSUserDefaults messing with SegmentedControl

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.

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.

EDIT2: Some code (sorry)

//This is the expenses class import UIKit

class Expense: NSObject, NSCoding {

//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")
    }
}

}

//The NSUserDefaultsManager import UIKit

class NSUserDefaultsManager: NSObject { static let userDefaults = NSUserDefaults.standardUserDefaults()

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
        }
    }
}

}

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.

ANSWER

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

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

while in viewDidLoad I added

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

Upvotes: 0

Views: 72

Answers (1)

Andy Lebowitz
Andy Lebowitz

Reputation: 1501

sorry I don't have enough reputation to comment. But could you show the code, and Im guessing you're not synchronizing something or just not pulling it up in ViewDidLoad, etc.

Upvotes: 0

Related Questions