gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-30 13:34:33 25 4
gpt4 key购买 nike

嗨,我有一个 String = ["one","two","Three"] 数组

数据 1,2,3 已经在数组中,我想将新数据附加到数组中,附加工作正常并且附加了第四个对象,但是在退出 View 并返回后它又消失了,女巫的意思是它不会保存它,经过长时间的谷歌搜索后,什么也没有出现。我该怎么办呢?如果 NSUserdefaults 字符串数组是唯一的解决方案?

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
}

}

最佳答案

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

:(

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

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

要将数据保存在 CoreData 中,您大致需要执行以下操作:

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

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

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

关于调用 .append 时,Swift 字符串数组不保存数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36227783/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com