我正在开发一个使用核心数据的应用程序包含多个列表(每个列表都是tabViewController上的一个单独的选项卡) . 现在我有一些问题列在下面 .

  • 对于所有用户输入的数据,只有一个具有多个属性的实体 .

  • 每个列表选项卡由bool隔离为true / false . 如果列表中有's true it',如果为false,则不是 .

  • 但是,如果我尝试通过fetchedResultsController和NSPredicate在另一个选项卡(另一个VC)上获取它们,则它们返回为nil,或者给出错误 not key coding compliant .

如何在另一个列表中返回属性数据以更改第一个列表中的数据 .

  • 示例:列表1包含项目名称和数量

  • list 2需要获取CoreData并识别列表1上有一个相同名称的项目并减去列表2 's qty from list 1' s项目的数量 .

关于为什么这不起作用的任何想法/知道如何解决它?提前致谢

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等一样创建 .

非常感谢您的帮助!