(Swift) Storing and retrieving Array to NSUserDefaults

前端 未结 7 970
别跟我提以往
别跟我提以往 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:44

    From your code I see you are storing some array

    // Your code
    NSUserDefaults.standardUserDefaults().setObject(myArray, forKey: "\(identity.text!)listA")
    

    and retrieving a string

    //Your code
    let tabledata = NSUserDefaults.standardUserDefaults().stringForKey("\(identity.text!)listA")
    

    There is probably a type mismatch, You store one type and retrieve another type.

    While retrieving either use arrayForKey() or objectForKey() see the code below.

    let tabledata = NSUserDefaults.standardUserDefaults().arrayForKey("\(identity.text!)listA") 
    

    or

    let tabledata = NSUserDefaults.standardUserDefaults().objectForKey("\(identity.text!)listA")
    

    If it is an array I would go with First one.

    0 讨论(0)
提交回复
热议问题