Epic Gamer_1
Epic Gamer_1

Reputation: 114

Swift 2.2 - NSUserDefaults not working and adding to list iOS

When I use NSUserDefaults, it is not working and reloading the data on the tableView. I have a page to add an item, but it shows the item added, and when I open it again, it is not saving. here is my full code.

import UIKit

var noteTitles = [String]()
var text = [String]()

class TableViewController: UITableViewController {

  override func viewDidAppear(animated: Bool) {
    tableView.reloadData()
  }

  override func viewDidLoad() {
    super.viewDidLoad()

    if NSUserDefaults.standardUserDefaults().objectForKey("note") != nil {

      noteTitles = NSUserDefaults.standardUserDefaults().objectForKey("note") as! [String]

    }

    // Uncomment the following line to preserve selection between presentations
    // self.clearsSelectionOnViewWillAppear = false

    // Uncomment the following line to display an Edit button in the navigation bar for this view controller.
    // self.navigationItem.rightBarButtonItem = self.editButtonItem()
  }

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

  // MARK: - Table view data source

  override func numberOfSectionsInTableView(tableView: UITableView) -> Int {
    // #warning Incomplete implementation, return the number of sections
    return 1
  }

  override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    // #warning Incomplete implementation, return the number of rows
    return noteTitles.count
  }


  override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
    let cell = tableView.dequeueReusableCellWithIdentifier("Cell", forIndexPath: indexPath) as! UITableViewCell

    cell.textLabel?.text = noteTitles[indexPath.row]

    // Configure the cell...

    return cell
  }

I don't know what is going wrong, and Xcode does not show any errors or warnings. What did I do wrong, and how would I fix it?

Upvotes: 0

Views: 205

Answers (2)

Hasya
Hasya

Reputation: 9898

If you are working with NSUserDefaults at many places.

You could use below structure.

In AppDelegate

func saveToDefaults(ObjectToSave : AnyObject?  , KeyToSave : String)
{

    let defaults = NSUserDefaults.standardUserDefaults()

    if (ObjectToSave != nil)
    {

        defaults.setObject(ObjectToSave, forKey: KeyToSave)
    }
    else
    {
        defaults.removeObjectForKey(KeyToSave)
    }

    NSUserDefaults.standardUserDefaults().synchronize()
}

func getFromDefaults(KeyToReturnValye : String) -> AnyObject?
{
    let defaults = NSUserDefaults.standardUserDefaults()

    if let name = defaults.valueForKey(KeyToReturnValye)
    {
        return name
    }
    return nil
}

struct defaultKeys
{
    static let key_note = “notes”
    . . . 
    . . . 
}

Throughout application development whenever you want to access defaults. you could use code like below.

let appDelegate = UIApplication.sharedApplication().delegate as! AppDelegate

appDelegate.saveToDefaults(“Value”, KeyToSave: appDelegate.defaultKeys.key_note)
appDelegate.getFromDefaults(appDelegate.defaultKeys.key_note)

Upvotes: 0

ronan
ronan

Reputation: 1611

I don't see where you saved your value.

You have to save it first, then call synchronize method so that value can be saved.

NSUserDefaults.standUserDefaults().setObject(noteTitles, forKey: "note")
NSUserDefaults.standardUserDefaults().synchronize()

Upvotes: 1

Related Questions