调用.append时,Swift字符串数组不保存数据

时间:2016-03-25 20:48:34

标签: swift

嗨我有一个String = [“one”,“two”,“three”]

的数组

数据1,2,3已经在数组中了,我想将新数据追加到数组中,追加工作并附加第四个对象,退出视图后退出并再次返回,这意味着它没有保存, 在谷歌长时间搜索后没有出现任何问题。 我该怎么办呢? 如果NSUserdefaults String数组是唯一的解决方案?

var HouseArray :[String] = []
let CarArray :[String] = []
let PartyArray :[String] = []
let StudyArray :[String] = []
var FavoArray :[String] = []
let InsureArray :[String] = []
let ShoppingArray :[String] =  []
let CreditArray :[String] = []
let KidsArray :[String] = []

let SaveArray : [String] = []
let SaveData = NSUserDefaults.standardUserDefaults()

var ImageIs = "House"
var imgIndicator:Int = 0
var newStringIs:String!
 override func viewWillAppear(animated: Bool) {
    super.viewWillAppear(animated)
    //GetTodaysMonthAndYear()
    WitchImageSelected()
    tableView.reloadData()


}

func WitchImageSelected(){

    ImageIs = SaveData.valueForKey("Image") as! String!

    switch(ImageIs){
    case "House":
        imgIndicator = 0

    case "Car":
        imgIndicator = 1
    case "Party":
        imgIndicator = 2
    case "Cart":
        imgIndicator = 3
    case "CreditCard":
        imgIndicator = 4
    case "Study":
        imgIndicator = 5
    case "Insure":
        imgIndicator = 6
    case "Favo":
        imgIndicator = 7
    case "Kids":
        imgIndicator = 8
    default: break
    }

    if(SaveData.valueForKey("newString") != nil){
        newStringIs = SaveData.valueForKey("newString") as! String!
        SaveData.setValue(nil, forKey: "newString")

  HouseArray.append(newStringIs)

    }
}
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {

    let Index = self.tableView.indexPathForSelectedRow
    if segue.identifier == "EnterPrice"{
        switch(imgIndicator){
        case 0: SaveData.setValue("\(HouseArray[Index!.row])", forKey: "NameLabel")
        case 1: SaveData.setValue("\(CarArray[Index!.row])", forKey: "NameLabel")
        case 2: SaveData.setValue("\(PartyArray[Index!.row])", forKey: "NameLabel")
        case 3: SaveData.setValue("\(ShoppingArray[Index!.row])", forKey: "NameLabel")
        case 4: SaveData.setValue("\(CreditArray[Index!.row])", forKey: "NameLabel")
        case 5: SaveData.setValue("\(StudyArray[Index!.row])", forKey: "NameLabel")
        case 6: SaveData.setValue("\(InsureArray[Index!.row])", forKey: "NameLabel")
        case 7: SaveData.setValue("\(FavoArray[Index!.row])", forKey: "NameLabel")
        case 8: SaveData.setValue("\(KidsArray[Index!.row])", forKey: "NameLabel")

        default: break
        }




}
}
override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {


    switch(imgIndicator){
    case 0: return HouseArray.count
    case 1: return CarArray.count
    case 2: return PartyArray.count
    case 3: return ShoppingArray.count
    case 4: return CreditArray.count
    case 5: return StudyArray.count
    case 6: return InsureArray.count
    case 7: return FavoArray.count
    case 8: return KidsArray.count

    default: return 0
    }

}

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

    switch(imgIndicator){
    case 0: SlideOutCell.textLabel?.text = HouseArray[indexPath.row]
    case 1: SlideOutCell.textLabel?.text = CarArray[indexPath.row]
    case 2: SlideOutCell.textLabel?.text = PartyArray[indexPath.row]
    case 3: SlideOutCell.textLabel?.text = ShoppingArray[indexPath.row]
    case 4: SlideOutCell.textLabel?.text = CreditArray[indexPath.row]
    case 5: SlideOutCell.textLabel?.text = StudyArray[indexPath.row]
    case 6: SlideOutCell.textLabel?.text = InsureArray[indexPath.row]
    case 7: SlideOutCell.textLabel?.text = FavoArray[indexPath.row]
    case 8: SlideOutCell.textLabel?.text = KidsArray[indexPath.row]

    default: break
    }



    return SlideOutCell
}

}

1 个答案:

答案 0 :(得分:0)

当您的视图消失时,ViewController对象将从内存中释放,这意味着其中的所有内容都将丢失。您几乎无法找回丢失的数据。

:(

这就是为什么你需要在vc被解除分配之前将数据保存到某个地方。您可以将其保存在CoreData或NSUserDefaults中。我推荐前者。

NSUserDefaults应该用于保存用户首选项/设置,因此不适合将案例中的数据保存到NSUserDefaults。特别是当阵列越来越大时。

要在CoreData中保存数据,这里大概是您需要做的:

  • 创建核心数据数据模型
  • 设计您的实体。我认为这很简单,只需添加一个实体并添加一个名为string的属性。
  • 生成NSManagedObject子类
  • 获取托管对象上下文,您可以开始查询“数据库”,尽管它只是一个对象模型

要了解更多信息,请参阅此tutotial:

https://www.raywenderlich.com/934/core-data-tutorial-for-ios-getting-started