-2

I am developing a Note app. I have developed a save button that when is pressed it saves the note with a custom name into NSUserDefaults.

I can fetch the contents of the notes however, I cannot figure out how to get a list of the notes saved names.

For examples if a user saved 3 notes custom named like: dogs, cats, food how can I fetch this saved named into a UITableview?

To save the notes I use:

var noteToSave: NSString = noteResult.text;
NSUserDefaults.standardUserDefaults().setObject(noteToSave, forKey: nameSave.text)
NSUserDefaults.standardUserDefaults().synchronize()

to fetch all keys:

var defaults: NSUserDefaults = NSUserDefaults.standardUserDefaults()
        var defaultAsDic: [NSObject : AnyObject] = defaults.dictionaryRepresentation()
        var keyArr: [AnyObject] = defaultAsDic.allKeys()
        for key: String in keyArr {
            NSLog("key [%@] => Value [%@]", key, defaultAsDic.valueForKey(key))

but its not working

SNos
  • 3,430
  • 5
  • 42
  • 92
  • Not sure what are you asking. – Dharmesh Kheni Sep 14 '15 at 17:29
  • I want to show the Keys of the notes saved into a UITableview - I have tried with println(NSUserDefaults.standardUserDefaults().dictionaryRepresentation().keys.array) however the app crashes – SNos Sep 14 '15 at 17:31
  • Unclear what this is asking. Is the question how to you populate table views? Or how do you fetch things saved in NSUserDefaults? – rudd Sep 14 '15 at 17:34
  • You have save your note names somewhere first after that you can read it. – Dharmesh Kheni Sep 14 '15 at 17:35
  • lets say how to fetch all the keys saved in NSUserDefaults – SNos Sep 14 '15 at 17:35
  • `NSTableView`? If you are asking for someone to provide code this is the wrong place, SO is about helping developers with their code, add the code you are having trouble with. – zaph Sep 14 '15 at 17:35
  • Sorry forgot to add the fetching code.. see my edit – SNos Sep 14 '15 at 17:39
  • 1
    FYI - `NSUserDefaults` is not the proper place to store your data. Write to a file or a database or something more appropriate than `NSUserDefaults`. – rmaddy Sep 14 '15 at 17:40

2 Answers2

0

To get noteResult.text you should add this code:

 NSUserDefaults.standardUserDefaults().stringForKey("\(identity.text!)")
ridvankucuk
  • 2,407
  • 1
  • 23
  • 41
0

Here is simple example for you:

var jo = [
        "a" : "1.0",
        "b" : "2.0"
    ]
//Store your Dictionary into userDefaults.  
let userDefaults = NSUserDefaults.standardUserDefaults()
userDefaults.setObject(jo, forKey: "yourKey")

//Get keys from dictionary
var allKeys = [String]()
if let data0 = userDefaults.dictionaryForKey("yourKey") {
    for (key, value) in data0 {
        println("\(key) -> \(value)")
        allKeys.append(key as! String)
    }
} else {
    println("not set")
}

println(allKeys) //[b, a]

Now you can display this array into your tableView.

Here is original answer: Swift NSUserDefaults not saving Dictionary?

And saving notes into NSUserDefaults is not a good way to store your data.Instead of that you can use other tools like plist, SQLite, etc.

Community
  • 1
  • 1
Dharmesh Kheni
  • 71,228
  • 33
  • 160
  • 165