-2

Here is my code. It seems an error. Can anyone please help.

class PopUpVC: UIViewController {
  var myArray1 = ["1","2","3"]
  var myArray2 = [String]()
  UserDefaults.standard.set(myArray1, forKey: "array")

  override func viewDidLoad() {
    super.viewDidLoad()
    self.myArray2 = (UserDefaults.standard.string(forKey: "array") as? [String])!
    print(self.myArray)
  }
Wide Angle Technology
  • 1,184
  • 1
  • 8
  • 28

1 Answers1

1
class PopUpVC: UIViewController {
  var myArray1 = ["1","2","3"]
  var myArray2 = [String]()

 override func viewDidLoad() {
      super.viewDidLoad()
      UserDefaults.standard.set(myArray1, forKey: "array")
      self.myArray2 = UserDefaults.standard.stringArray(forKey: "array")!
      print(self.myArray2)
 }
Wide Angle Technology
  • 1,184
  • 1
  • 8
  • 28
Krishnarjun Banoth
  • 1,410
  • 1
  • 15
  • 30
  • That answer is just wrong. You don't retrieve an Array of Strings using `Userdefaults.string(forKey:)`, you do it using `array(forKey:)` or `object(forKey:)`, then cast it to `[String]`. Have you even tested that code? It wouldn't even compile, since you are trying to give a `[String]` default value to a `String?` variable. – Dávid Pásztor Aug 30 '17 at 10:14
  • i have used is not `String(forKey:)` I used is `StringArray(forKey:)` – Wide Angle Technology Aug 31 '17 at 11:29
  • Yeah that's correct i was accepted your edit. – Krishnarjun Banoth Aug 31 '17 at 12:07