【发布时间】:2015-05-15 17:15:35
【问题描述】:
我是 iOS 开发的新手,想知道我应该指定哪种数据类型来存储多个字符串(数组)。该应用程序与食物有关,我需要将多种成分存储为一个属性。
我正在考虑将成分制作为实体,但我只是想让初学者更容易。 我读过可转换类型,但人们似乎不建议使用它来存储数组。
【问题讨论】:
我是 iOS 开发的新手,想知道我应该指定哪种数据类型来存储多个字符串(数组)。该应用程序与食物有关,我需要将多种成分存储为一个属性。
我正在考虑将成分制作为实体,但我只是想让初学者更容易。 我读过可转换类型,但人们似乎不建议使用它来存储数组。
【问题讨论】:
警告:前面有自以为是的答案。
你没有。
将事物存储在数组中不会让您更轻松。相反,它只会在一个小时内使事情变得更加困难。想象一下,您想显示所有包含选定成分的食谱。使用您的数组破解并不容易,使用合适的模型只需几行代码。
我建议使用“加入实体”的良好旧关系。
是的,这比拼凑一些几乎不起作用的东西要复杂得多。但这是正确的方法。
【讨论】:
你所想的正是你应该做的。 Core Data 用于将值存储在类似数组的结构中。您应该创建实体 Ingredients 并将您的 Food 实体(或任何您喜欢的名称)与 Ingredients 实体的关系联系起来。
【讨论】:
有办法。您可以手动执行每个元素,例如 你有你的数组:
let employee: NSMutableArray = []
employee.addObject(["name":"Bill","LastName":"Hanks"])
employee.addObject(["name":"Rolex","LastName":"Swarzer"])
employee.addObject(["name":"Clive","LastName":"Martin"])
employee.addObject(["name":"Jimi","LastName":"Hendrix"])
假设您已使用实体“Employee”和属性“name”和“lastname”创建了 coreData,您执行以下操作将其添加到...
let appDel = UIApplication.sharedApplication().delegate as! AppDelegate
let context = appDel.managedObjectContext
for item in employee {
do {
let newUser = NSEntityDescription.insertNewObjectForEntityForName("Employee", inManagedObjectContext: context)
newUser.setValue(item["name"], forKey: "name")
newUser.setValue(item["LastName"], forKey: "lastname")
try context.save()
} catch {
//do nothing
}
然后您可以使用您的获取请求或 NSFetched 结果控制器获取所有元素
【讨论】:
我已经在 Swift 4 中完成了,
将更多数组存储到 allDataArray(一个数组)。从CoreData(AllData)获取数组对象并在TableView中显示
import UIKit
import Foundation
import CoreData
class ViewController: UIViewController {
var allTableDataArray : [AllData] = [AllData]()
let allDataArray : NSMutableArray = []
var listOfArray1 = ["#849578", "#849302"]
var listOfArray2 = ["Vasuki Shiv", "Prathap Dusi"]
override func viewDidLoad() {
super.viewDidLoad()
saveAllDataToCoredata()
}
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(true)
fetchAllDataFromCoredata()
}
func saveAllDataToCoredata() {
deleteAllData(entity: "AllData")
let context = PersistenceSerivce.context
allDataArray.add(["requestNo" : listOfArray1[0], "vendorName" : listOfArray2[0]])
allDataArray.add(["requestNo" : listOfArray1[1] , "vendorName" : listOfArray2[1]])
for item in (allDataArray){
do {
let newUser = NSEntityDescription.insertNewObject(forEntityName: "AllData", into: context)
guard let requestNoNew = item as? [String:Any] else {
return
}
let requestNoStr = requestNoNew["requestNo"] as! String
newUser.setValue(requestNoStr, forKey: "requestNo")
guard let vendorNameNew = item as? [String:Any] else {
return
}
let vendorNameStr = vendorNameNew["vendorName"] as! String
newUser.setValue(vendorNameStr, forKey: "vendorName")
PersistenceSerivce.saveContext()
try context.save()
} catch {
//do nothing
}
}
}
func fetchAllDataFromCoredata(){
let context = PersistenceSerivce.context
let fetchRequest = NSFetchRequest<AllData>(entityName: "AllData")
allTableDataArray.removeAll()
do {
allTableDataArray = try context.fetch(fetchRequest)
} catch {
print("Unable to fetch from Coredata", error)
}
}
func deleteAllData(entity: String) {
let managedContext = PersistenceSerivce.context
let fetchRequest = NSFetchRequest<NSFetchRequestResult>(entityName: entity)
fetchRequest.returnsObjectsAsFaults = false
do
{
let results = try managedContext.fetch(fetchRequest)
for managedObject in results
{
let managedObjectData:NSManagedObject = managedObject as! NSManagedObject
managedContext.delete(managedObjectData)
}
} catch let error as NSError {
print("Delete all data in \(entity) error : \(error) \(error.userInfo)")
}
}
}
//MARK:- UITableView
extension ViewController : UITableViewDelegate, UITableViewDataSource {
func tableView(_ tableView: UITableView,
heightForRowAt indexPath: IndexPath) -> CGFloat {
return 44
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return (allTableDataArray.count)
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: “TableViewCellID”) as? TableViewCell
let allData = allTableDataArray[indexPath.row]
cell?.requestNoLabel.text = allData.requestNo
cell?.vendorNameLabel.text = allData.vendorName
return cell!
}
}
【讨论】: