0

I want to store string every time in array via NSUserDefaults. and i want to retrive that strings(set of string) it in a UITableView cell.

My code

enum defaultsKeys {
    static let keymodel = "firstStringKey"
    static let keyVersion = "secondStringKey"
    static let keyBodystyle = "thirdStringKey"
    static let keyTotalprice = "fourthStringKey"
    static let keyDate = "fifthStringKey"

}

override func viewDidLoad() {
    super.viewDidLoad()

    let defaults = NSUserDefaults.standardUserDefaults()
    defaults.setValue(mymodel, forKey: defaultsKeys.keymodel)
    defaults.setValue(myversion, forKey: defaultsKeys.keyVersion)
    defaults.setValue(mybodystyle, forKey: defaultsKeys.keyBodystyle)
    defaults.setValue(mytotalprice, forKey: defaultsKeys.keyTotalprice)
    defaults.setValue(mymodel, forKey: defaultsKeys.keyDate)

    // Do any additional setup after loading the view.
}

override func didReceiveMemoryWarning() {
    super.didReceiveMemoryWarning()
    // Dispose of any resources that can be recreated.
}


func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {

    let cell:selectedcarcell = tableView.dequeueReusableCellWithIdentifier("selectedcarcell") as! selectedcarcell


    let defaults = NSUserDefaults.standardUserDefaults()

    if let stringmodel = defaults.stringForKey(defaultsKeys.keymodel) {
        cell.carname.text = stringmodel
    }

    if let stringversion = defaults.stringForKey(defaultsKeys.keyVersion) {
        cell.carversion.text = stringversion
    }

    if let stringbodystyle = defaults.stringForKey(defaultsKeys.keyBodystyle) {
        cell.carbodystyle.text = stringbodystyle
    }

    if let stringfinalprice = defaults.stringForKey(defaultsKeys.keyTotalprice) {
       cell.finalprice.text = stringfinalprice
    }

    if let stringdate = defaults.stringForKey(defaultsKeys.keyDate) {
        cell.date.text = stringdate
    }

    return cell

}

I am doing this for string but i want to do this for array so how can I do this

Here is my output enter image description here

But when second time i insert nother values my previous values are not coming in array. so how can i do this i also want all previous value?

Rashwan L
  • 38,237
  • 7
  • 103
  • 107
Govind Rakholiya
  • 427
  • 6
  • 24

1 Answers1

2

With a little research you would have found out, it works in a very similar way:

Saving an array:

let defaults = NSUserDefaults.standardUserDefaults()
let array = ["1", "2"]
defaults.setObject(array, forKey: "array")

Retrieving an array:

let array = defaults.objectForKey("array")
print(array)


If you want to append something to the array each time you run the app, use something like this:

var array = NSUserDefaults.standardUserDefaults().objectForKey("Array") as? [AnyObject]
if array == nil { // This will only happen the first time the user opens the app
    array = [AnyObject]()
}
array!.append("someobject")
NSUserDefaults.standardUserDefaults().setObject(array, forKey: "Array")

You can then display each element in a UITableViewCell, if you change your cellForRowAtIndexPath to look like this:

cell.carname.text = array[indexPath.row]
Atomix
  • 13,427
  • 9
  • 38
  • 46
  • thanks @JoJoe i know this but my problem is every time its showing only one value so how can i display previous value.? – Govind Rakholiya Dec 24 '15 at 11:06
  • I'm not sure what you mean by that. Do you want to display each element of the array in a different UITableViewCell? – Atomix Dec 24 '15 at 12:20
  • you are not getting my question.. first time i append two value "1" and "2" and i display it in uitableview. now when second time my app run i insert value "3" and "4" so at that time i only getting value "3" and "4" but i want all four values"1","2","3" and "4" so how can i get this?? Thanx in advance – Govind Rakholiya Dec 24 '15 at 12:33
  • I edited the answer. Does this help you? – Atomix Dec 25 '15 at 20:07