我正在开发一个使用核心数据的应用包含多个列表(每个列表都是tabViewController上的单独选项卡)。现在我有一些问题列在下面。
如何让属性数据在另一个列表中返回,以便更改第一个列表中的数据。
关于为什么这不起作用的任何想法/知道如何解决它?提前谢谢
FetchRequest(在列表2上):
func itemFetchRequest1() -> NSFetchRequest{
let fetchRequest = NSFetchRequest(entityName: "List")
fetchRequest.predicate = NSPredicate(format:"pitem =%@", item.pitem!)
return fetchRequest
}
做数学运算:
func subtractPqty(){
if (item.ringredients == item.pitem){
//get value of string
let stringNumber0 = item.rqty0
let stringNumber1 = item.pqty
//convert string to Int
let numberFromString0 = Int(stringNumber0!)
let numberFromString1 = Int(stringNumber1!)
//get sum of Int
let sum = (numberFromString1)! - (numberFromString0)!
let myInt:Int = sum
//convert back Int back to string
let myString:String = String(myInt)
//delclare string as qty.
item.pqty = myString
}else{
print(item.pitem)
print(item.ringredients)
}
}
如果看到创建项目的func更有意义,我也将其包括在内。
func createNewitem() {
guard self.item == nil else {
print("trying to create a new item while there is already one.")
return
}
// just creating an empty item and let give the job to filling it to editItem method.
let entityDescription = NSEntityDescription.entityForName("List", inManagedObjectContext: moc)
// assign the empty item to self.item.
let item = List(entity: entityDescription!, insertIntoManagedObjectContext: moc)
item.mplist = true
item.mpcross = false
// assign the new item to self.item
self.item = item
item.mpitem = Recipe.text
item.mpcategory = mealOfDay.text
item.ringredients = recipeItem.text
item.mpdate = dayOfWeek.text
item.rqty0 = rqty.text
itemFetchRequest1()
func subtractPqty()
}
第一个列表与plist,pcross,pitem等创建相同。
非常感谢您的帮助!