swift中的核心数据数组
Posted
技术标签:
【中文标题】swift中的核心数据数组【英文标题】:Core Data Array in swift 【发布时间】:2016-01-25 20:36:38 【问题描述】:大家好,我有一个问题。看了很多视频后,我不明白如何使用Core Data
来存储数组。
我有一个应用程序,我在视图中放置了 16 TextLabel
。我需要使用Core Data
来存储字符串列表。当我启动程序时,我需要从数组中加载数据。但有时我需要用文本字段中的文本更改一个TextLabel
的数据,然后重新加载所有16 个文本标签。
有人可以帮我写代码吗?
抱歉,我不明白它是如何工作的。
我已经使用核心数据创建了项目。 非常感谢
【问题讨论】:
我们希望帮助您编写代码,但请与我们分享您已有的代码,以便我们从某个地方开始。 ***.com/questions/4546811/… 【参考方案1】:第一个建议:用tag
s 的标签替换所有网点。这将产生更少的代码,从而更具可读性。您可以使用方便的循环来填写标签。
例如,您的“主要”标签可能有标签 10-17, 您的“次要”标签可能有标签 20-27。
要获得特定标签,只需使用
let label = view.viewWithTag(20) as! UILabel
其次,对于存储 16 个字符串的数组,请使用 NSUserDefaults
,它更简单,并且专为这种数据量和类型而构建。
NSUserDefaults.standardUserDefaults().setObject(array, forKey: "myArray")
【讨论】:
【参考方案2】:这是我的代码。在此视图中,我想使用数组设置所有 16 个文本标签。当我按下这些文本标签之一下的按钮时,我需要使用 granigliatoreTF 中的字符串更改数据,然后重新加载所有 16 个文本标签。我只实现了一个按钮开始。谢谢大家
导入 UIKit 导入核心数据
类 DisposizioneGranigliatori: UIViewController, UIPickerViewDelegate, UIPickerViewDataSource
var disposizione = [NSManagedObject]()
@IBOutlet var granigliatoreTF: UITextField!
@IBOutlet var principale1LB: UILabel!
@IBOutlet var principale2LB: UILabel!
@IBOutlet var principale3LB: UILabel!
@IBOutlet var principale4LB: UILabel!
@IBOutlet var principale5LB: UILabel!
@IBOutlet var principale6LB: UILabel!
@IBOutlet var principale7LB: UILabel!
@IBOutlet var principale8LB: UILabel!
@IBOutlet var secondario1LB: UILabel!
@IBOutlet var secondario2LB: UILabel!
@IBOutlet var secondario3LB: UILabel!
@IBOutlet var secondario4LB: UILabel!
@IBOutlet var secondario5LB: UILabel!
@IBOutlet var secondario6LB: UILabel!
@IBOutlet var secondario7LB: UILabel!
@IBOutlet var secondario8LB: UILabel!
@IBOutlet var modifiche: UIView!
@IBOutlet weak var picker: UIPickerView!
var pickerData : [[String]] = [[String]]()
override func viewDidLoad()
super.viewDidLoad()
modifiche.hidden = true
pickerData = [["12","161","164","165","176","177","245","246","247","250","255","256","262","263","266","267","268","269","270","271","274","275","278","287","289","290","293","301","303","306","307","308","328","330","336","337","340","344","346","347","352","355","357","374","377","381","383","387","391","394","395","399","400","401","403","417","421","423","430","F365","TGF 2666","FTS 2061"],["00","0,09","0,10","0,12","0,12-00","1400","Z","0,14-0,12","0,14","0,16","0,18","0,21","0,25","0,31","0,40","0,52","0,71"]]
self.picker.delegate = self
self.picker.dataSource = self
override func didReceiveMemoryWarning()
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
// The number of columns of data
func numberOfComponentsInPickerView(pickerView: UIPickerView) -> Int
return pickerData.count
// The number of rows of data
func pickerView(pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int
return pickerData[component].count
// The data to return for the row and component (column) that's being passed in
func pickerView(pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String?
return pickerData[component][row]
@IBAction func bigbagPrinc1(sender: AnyObject)
@IBAction func bigbagSec1(sender: AnyObject)
【讨论】:
你应该在你的问题中包含这个。使用问题下方的“编辑”链接。以上是关于swift中的核心数据数组的主要内容,如果未能解决你的问题,请参考以下文章