(Swift) Storing and retrieving Array to NSUserDefaults

前端 未结 7 968
别跟我提以往
别跟我提以往 2020-11-30 05:43

I am trying to store an array to NSUserDefaults and retrieve the array when needed to populate a UITableView.

Currently I am using:

<
相关标签:
7条回答
  • 2020-11-30 06:18

    Saving and Retrieving a String array to UserDefaults

    Save array

    let array = ["horse", "cow", "camel", "sheep", "goat"]
    
    let defaults = UserDefaults.standard
    defaults.set(array, forKey: "SavedStringArray")
    

    Retrieve array

    let defaults = UserDefaults.standard
    let array = defaults.object(forKey: "SavedStringArray") as? [String] ?? [String]()
    

    See more at How to save user settings using NSUserDefaults

    0 讨论(0)
  • 2020-11-30 06:25

    Latest swift3 version to store array of data into userdefaults and get that array of data using keys

        let nameArray = ["Ramu","JAGAN","Steve","Swift"]
        let namesArrayData = NSKeyedArchiver.archivedData(withRootObject: nameArray)
        UserDefaults.standard.set(namesArrayData, forKey: "arrayData")
    

    RetriveData

            let retriveArrayData= UserDefaults.standard.object(forKey:  "arrayData") as? NSData
    
       if let retriveArrayData= namesArrayData{
      let retriveArray = NSKeyedUnarchiver.unarchiveObject(with: namesArraydata as Data) as? [nameArray]
      print(retriveArray )
    }
    
    0 讨论(0)
  • 2020-11-30 06:25

    This has been changed in swift 3. Note that this works with device but not playground or simulator

    //store the array
    let array = ["horse", "cow", "camel", "sheep", "goat"]
    
    let defaults = UserDefaults.standard
    
    defaults.setValue(array, forKey: "history")
    
    defaults.synchronize()
    
    //Retrieve the array
    if((UserDefaults.standard.array(forKey: "history")) != nil){
        let historyWords = (UserDefaults.standard.array(forKey: "history") as? [String])!
        print(historyWords)
    }
    
    0 讨论(0)
  • 2020-11-30 06:27

    What are you storing in the array?

    If it's objects it won't work, if It's just strings or numbers then it should be fine.

    Next steps you take should be to turn on exception breakpoints and maybe println() everything you can so you can pinpoint the exact whereabouts of the problem.

    As a shot in the dark I would suggest maybe (identity.text!) is coming out as nil

    Also you should probably change it to this

    if let tabledata: NSArray = NSUserDefaults.standardUserDefaults().stringForKey("\(identity.text!)listA"){
    
        myArray = [tabledata!]
    
        tableView.reloadData()
    }
    

    This will make the code not even try to run unless there is a value it can use

    0 讨论(0)
  • 2020-11-30 06:29

    Here are the way that you can store and retrieved array of object with help of NSKeyArchive.

    To Store array to NSUserDefault.

    let placesData = NSKeyedArchiver.archivedData(withRootObject: placesArray)
    UserDefaults.standard.set(placesData, forKey: "places")
    

    To Retrieved array from NSUserDefault.

    let placesData = UserDefaults.standard.object(forKey: "places") as? NSData
    
    if let placesData = placesData {
        let placesArray = NSKeyedUnarchiver.unarchiveObject(with: placesData as Data) as? [Place]
        print(placesArray)
    }
    

    Hope this help you.

    0 讨论(0)
  • 2020-11-30 06:40

    Store into User Default:

    let arrayFruit = ["Apple","Banana","Orange","Grapes","Watermelon"]    
     //store in user default
    UserDefaults.standard.set(arrayFruit, forKey: "arrayFruit")
    

    Retrieving array from User Defaults:

    if let arr = UserDefaults.standard.array(forKey: "arrayFruit") as? [String]{
        print(arr)
    }
    
    0 讨论(0)
提交回复
热议问题