在iOS Swift中将数据添加到CoreData中的特定表

时间:2017-08-04 15:04:20

标签: ios swift core-data

我在我的项目中使用CoreData,我成功地将数据保存在表格中并显示。我也能够更新现有数据。但我无法将新数据添加到特定的表格中。enter image description here

表关系是WorkOrderData - >使用WorkOrderTaskAsset和WorkOrderTaskAsset进行一对多 - >有条件的一对多。

如果我必须在某些按钮上添加条件对象。我没有得到如何添加它。我试图创建一个条件对象并添加它。但我收到了错误 .Condition setLabel:]:无法识别的选择器发送到实例0x600000077540'

func saveDataInCoreData(jsonArray : NSArray)  {
    for orders in jsonArray{

        let WorkOrderDataClassName:String = String(describing: WorkOrderData.self)
        let workOrderData:WorkOrderData = NSEntityDescription.insertNewObject(forEntityName: WorkOrderDataClassName, into: DatabaseController.getContext()) as! WorkOrderData

        let ContractorClassName:String = String(describing: Contractor.self)
        let contractor:Contractor = NSEntityDescription.insertNewObject(forEntityName: ContractorClassName, into: DatabaseController.getContext()) as! Contractor

        let StatusClassName:String = String(describing: Status.self)
        let status:Status = NSEntityDescription.insertNewObject(forEntityName: StatusClassName, into: DatabaseController.getContext()) as! Status





        let order = orders as! NSDictionary

        // Work Order Parsing
        workOrderData.workOrderNo = order["workOrderNo"]! as? String
        if order["description"] != nil {
        workOrderData.descriptin = order["description"]! as? String
        }

        if order["comments"] != nil {
            workOrderData.comments = order["comments"]! as? String

        }

        let desCodeId = order["descriptionCodeID"]! as? Int
        if desCodeId != nil {
            workOrderData.descriptionCodeID = Int16(UInt16(desCodeId!))
        }

        if order["definition"] != nil {
            workOrderData.definition = order["definition"]! as? String

        }

        let priortyID = order["priorityID"]! as? Int
        if priortyID != nil {
            workOrderData.priorityID = Int16(UInt16(desCodeId!))
        }

        let numbrOfAssets = order["numberofAssests"]! as? Int
        if numbrOfAssets != nil {
            workOrderData.numberofAssests = Int16(UInt16(numbrOfAssets!))
        }



        if order["generatedDateTime"] != nil {
            workOrderData.generatedDateTime = (order["generatedDateTime"]! as? Double)!
        }
        if order["initiatedBy"] != nil {
            workOrderData.initiatedBy = order["initiatedBy"]! as? String
        }
        if order["generatedBy"] != nil {
            workOrderData.generatedBy = order["generatedBy"]! as? String
        }

        if order["issueDate"] != nil {
            workOrderData.issueDate = (order["issueDate"]! as? Double)!
        }

        if order["finishDate"] != nil {
         workOrderData.finishDate = (order["finishDate"]! as? Double)!
        }



        if order["statusID"] != nil {
            let statsID = order["statusID"]! as? Int
            workOrderData.statusID = Int16(UInt16(statsID!))
        }


        if order["assigneeID"] != nil {
            let assigneID = order["assigneeID"]! as? Int
            workOrderData.assigneeID = Int16(UInt16(assigneID!))
        }



        if order["updateDate"] != nil {
            let updatDate = order["updateDate"]! as? Double
            workOrderData.updateDate = updatDate!
        }
        if order["startDate"] != nil {
            workOrderData.startDate = (order["startDate"]! as? Double)!
        }
        if order["proceedDate"] != nil {
            workOrderData.proceedDate = (order["proceedDate"]! as? Double)!
        }

        if order["createDate"] != nil {
            workOrderData.createDate = (order["createDate"]! as? Double)!

        }


        if order["inclementDays"] != nil {
            let inclemntDays = order["inclementDays"]! as? Int
            workOrderData.inclementDays = Int16(UInt16(inclemntDays!))
        }


        if order["totalHydrantsPainted"] != nil {
            let totalHydrantPainted = order["totalHydrantsPainted"]! as? Int
            workOrderData.totalHydrantsPainted = Int16(UInt16(totalHydrantPainted!))
        }


        if order["totalHydrantsNotPainted"] != nil {
            let totalHydrantNotPainted = order["totalHydrantsNotPainted"]! as? Int
            workOrderData.totalHydrantsNotPainted = Int16(UInt16(totalHydrantNotPainted!))
        }


        if order["contractorId"] != nil {
            let contractrId = order["contractorId"]! as? Int
            workOrderData.contractorId = Int16(UInt16(contractrId!))
        }

        // Contractor Started

        let contractorDict = order["contractor"]! as? NSDictionary


        if contractorDict?["id"] != nil {
            let contrctrId = contractorDict?["id"]! as? Int
            contractor.id = Int16(UInt16(contrctrId!))
        }
        contractor.name = contractorDict?["name"]! as? String
        contractor.email = contractorDict?["email"]! as? String
        contractor.label = contractorDict?["label"]! as? String
        contractor.purchaseOrderNumber = contractorDict?["purchaseOrderNumber"]! as? String
        contractor.vendorId = contractorDict?["vendorId"]! as? String
        workOrderData.contractor = contractor

        if order["status"] != nil {
            let statusDict = order["status"]! as? NSDictionary

            let statusId = statusDict?["id"]! as? Int
            if statusId != nil {
                status.id = Int16(UInt16(statusId!))
            }
            status.label = statusDict?["label"]! as? String
            status.descriptin = statusDict?["description"]! as? String
            status.rule = statusDict?["rule"]! as? String

            workOrderData.status = status
        }


        // Inclement Work Order  Started

        if order["workOrderInclementWeatherDates"]  != nil {
            let workOrderInclementWeatherDate = order["workOrderInclementWeatherDates"] as! [NSDictionary]

            if workOrderInclementWeatherDate.count > 0 {
                let setArray = NSMutableArray()
                let set = NSMutableSet()
                for inclementItem in workOrderInclementWeatherDate {

                    let WorkOrderInclementWeatherDatesClassName:String = String(describing: WorkOrderInclementWeatherDates.self)
                    let workOrderInclementWeatherDates:WorkOrderInclementWeatherDates = NSEntityDescription.insertNewObject(forEntityName: WorkOrderInclementWeatherDatesClassName, into: DatabaseController.getContext()) as! WorkOrderInclementWeatherDates

                    workOrderInclementWeatherDates.createTimeStamp = inclementItem.value(forKey: "createTimeStamp") as! Double
                    workOrderInclementWeatherDates.inclementDate = inclementItem.value(forKey: "inclementDate") as! Double
                    workOrderInclementWeatherDates.workOrderNo = inclementItem.value(forKey: "workOrderNo") as? String

                    setArray.add(workOrderInclementWeatherDates)
                    set.add(workOrderInclementWeatherDates)


                }
                workOrderData.addToWorkOrderInclementWeather(set as NSSet)

            }



        }

        // Task List

        // Inclement Work Order  Started

        if order["workOrderTask"]  != nil {
            let workOrderTaskArray = order["workOrderTask"] as! [NSDictionary]

            if workOrderTaskArray.count > 0 {
                let workOrderSet = NSMutableSet()
                for workOrderItem in workOrderTaskArray {

                    let WorkOrderTaskClassName:String = String(describing: WorkOrderTask.self)
                    let workOrderTask:WorkOrderTask = NSEntityDescription.insertNewObject(forEntityName: WorkOrderTaskClassName, into: DatabaseController.getContext()) as! WorkOrderTask

                    workOrderTask.woTaskDescription = workOrderItem.value(forKey: "woTaskDescription") as? String

                    workOrderTask.taskStatus = workOrderItem.value(forKey: "taskStatus") as? String
                    if workOrderItem["projectedStartDate"] != nil {
                        workOrderTask.projectedStartDate = workOrderItem.value(forKey: "projectedStartDate") as! Double
                    }
                    if workOrderItem["actualStartDate"] != nil {
                        workOrderTask.actualStartDate = workOrderItem.value(forKey: "actualStartDate") as! Double
                    }


                    if workOrderItem["taskNumber"] != nil {
                        let taskNumber = workOrderItem["taskNumber"]! as? Int
                        workOrderTask.taskNumber = Int16(UInt16(taskNumber!))
                    }

                    if workOrderItem["noOfHydrantsCompleted"] != nil {
                        let noOfHydrantsCompleted = workOrderItem["noOfHydrantsCompleted"]! as? Int
                        workOrderTask.noOfHydrantsCompleted = Int16(UInt16(noOfHydrantsCompleted!))
                    }

                    if workOrderItem["noOfHydrantsPending"] != nil {
                        let noOfHydrantsPending = workOrderItem["noOfHydrantsPending"]! as? Int
                        workOrderTask.noOfHydrantsPending = Int16(UInt16(noOfHydrantsPending!))
                    }

                    workOrderSet.add(workOrderTask)


                }
                workOrderData.addToWorkOrdertask(workOrderSet as NSSet)

            }



        }

        // workOrderTaskAsstDetails

        if order["workOrderTaskAsstDetails"]  != nil {
            let workOrderTaskAsstDetailsDict = order["workOrderTaskAsstDetails"] as! [NSDictionary]

            if workOrderTaskAsstDetailsDict.count > 0 {
                let set = NSMutableSet()
                for workOrderTaskAsstDetailsItem in workOrderTaskAsstDetailsDict {

                    let WorkOrderTaskAsstDetailsClassName:String = String(describing: WorkOrderTaskAsstDetails.self)
                    let workOrderTaskAsstDetails:WorkOrderTaskAsstDetails = NSEntityDescription.insertNewObject(forEntityName: WorkOrderTaskAsstDetailsClassName, into: DatabaseController.getContext()) as! WorkOrderTaskAsstDetails

                    let woTaskAsstId = workOrderTaskAsstDetailsItem["woTaskAsstId"]! as? Int
                    if woTaskAsstId != nil {
                        workOrderTaskAsstDetails.woTaskAsstId = Int16(UInt16(woTaskAsstId!))
                    }

                    workOrderTaskAsstDetails.woTaskNo = workOrderTaskAsstDetailsItem.value(forKey: "woTaskNo") as? String
                    workOrderTaskAsstDetails.assetId = workOrderTaskAsstDetailsItem.value(forKey: "assetId") as? String
                    workOrderTaskAsstDetails.descripton = workOrderTaskAsstDetailsItem.value(forKey: "description") as? String
                    workOrderTaskAsstDetails.statusID = workOrderTaskAsstDetailsItem.value(forKey: "statusID") as? String
                    if workOrderTaskAsstDetailsItem["projectedStartDate"] != nil {
                        workOrderTaskAsstDetails.projectedStartDate = (workOrderTaskAsstDetailsItem["projectedStartDate"]! as? Double)!

                    }
                    if workOrderTaskAsstDetailsItem["actualStartDate"] != nil {
                        workOrderTaskAsstDetails.actualStartDate = (workOrderTaskAsstDetailsItem["actualStartDate"]! as? Double)!

                    }
                    if workOrderTaskAsstDetailsItem["projectedCompleteDate"] != nil {
                        workOrderTaskAsstDetails.projectedCompleteDate = (workOrderTaskAsstDetailsItem["projectedCompleteDate"]! as? Double)!

                    }
                    if workOrderTaskAsstDetailsItem["actualCompleteDate"] != nil {
                        workOrderTaskAsstDetails.actualCompleteDate = (workOrderTaskAsstDetailsItem["actualCompleteDate"]! as? Double)!

                    }
                    if workOrderTaskAsstDetailsItem["lastPaintedDate"] != nil {
                        workOrderTaskAsstDetails.lastPaintedDate = (workOrderTaskAsstDetailsItem["lastPaintedDate"]! as? Double)!

                    }
                    if workOrderTaskAsstDetailsItem["workOrderStartDate"] != nil {
                        workOrderTaskAsstDetails.workOrderStartDate = (workOrderTaskAsstDetailsItem["workOrderStartDate"]! as? Double)!

                    }
                    if workOrderTaskAsstDetailsItem["createDate"] != nil {
                        workOrderTaskAsstDetails.createDate = (workOrderTaskAsstDetailsItem["createDate"]! as? Double)!

                    }
                    if workOrderTaskAsstDetailsItem["updateDate"] != nil {
                        workOrderTaskAsstDetails.updateDate = (workOrderTaskAsstDetailsItem["updateDate"]! as? Double)!

                    }
                    workOrderTaskAsstDetails.workOrderNumber = workOrderTaskAsstDetailsItem.value(forKey: "workOrderNumber") as? String
                    workOrderTaskAsstDetails.beforePicLatitude = workOrderTaskAsstDetailsItem.value(forKey: "beforePicLatitude") as? String
                    workOrderTaskAsstDetails.beforePicLongitude = workOrderTaskAsstDetailsItem.value(forKey: "beforePicLongitude") as? String
                    workOrderTaskAsstDetails.afterPicLatitude = workOrderTaskAsstDetailsItem.value(forKey: "afterPicLatitude") as? String
                    workOrderTaskAsstDetails.afterPicLongitude = workOrderTaskAsstDetailsItem.value(forKey: "afterPicLongitude") as? String

                    let transactionType = workOrderTaskAsstDetailsItem["transactionType"]! as? Int
                    if transactionType != nil {
                        workOrderTaskAsstDetails.transactionType = Int16(UInt16(transactionType!))
                    }
                    let assetValid = workOrderTaskAsstDetailsItem["assetValid"]! as? Int
                    if assetValid != nil {
                        workOrderTaskAsstDetails.assetValid = Int16(UInt16(assetValid!))
                    }

                    //Asset
                    let AssetClassName:String = String(describing: Asset.self)
                    let asset:Asset = NSEntityDescription.insertNewObject(forEntityName: AssetClassName, into: DatabaseController.getContext()) as! Asset

                    let assetDict = workOrderTaskAsstDetailsItem["asset"]! as? NSDictionary
                    asset.descripton = assetDict?["description"]! as? String
                    asset.type = assetDict?["type"]! as? String
                    asset.assetId = assetDict?["assetId"]! as? String
                    if assetDict?["asset200ft"] != nil {
                        asset.asset200ft = assetDict?["asset200ft"]! as? String
                    }


                    if  assetDict?["streetNumber"] != nil{
                        asset.streetNumber = assetDict?["streetNumber"]! as? String
                    }

                    asset.county = assetDict?["county"]! as? String
                    asset.latitude = assetDict?["latitude"]! as? String
                    asset.longitude = assetDict?["longitude"]! as? String
                    workOrderTaskAsstDetails.asset = asset

                    //Condition

                        if workOrderTaskAsstDetailsItem["assetConditions"] != nil{
                        let conditionDict = workOrderTaskAsstDetailsItem["assetConditions"] as! [NSDictionary]

                        if conditionDict.count > 0{
                        let conditinSet = NSMutableSet()

                        for assetConditionsDetailsDict in conditionDict {

                        let ConditionClassName:String = String(describing: Condition.self)
                        let condition:Condition = NSEntityDescription.insertNewObject(forEntityName: ConditionClassName, into: DatabaseController.getContext()) as! Condition

                        let assetConditionItem = assetConditionsDetailsDict["condition"] as! NSDictionary
                        condition.id = assetConditionItem["id"]as? String
                            condition.label = assetConditionItem["label"]as? String
                            condition.rule = assetConditionItem["rule"]as? String
                            condition.descriptin = assetConditionItem["description"]as? String
                            conditinSet.add(condition)


                            }
                            workOrderTaskAsstDetails.addToCondition(conditinSet as NSSet)

                         }
                }
                    set.add(workOrderTaskAsstDetails)



                }
                workOrderData.addToWorkOrderTaskAsstDetails(set as NSSet)

            }



        }

        // assetList


        if order["assetList"]  != nil {
            let workOrderTaskAsstDetailsDict = order["assetList"] as! [NSDictionary]

            if workOrderTaskAsstDetailsDict.count > 0 {
                let set = NSMutableSet()
                for workOrderTaskAsstDetailsItem in workOrderTaskAsstDetailsDict {

                    let WorkOrderTaskAsstDetailsClassName:String = String(describing: AssetList.self)
                    let workOrderTaskAsstDetails:AssetList = NSEntityDescription.insertNewObject(forEntityName: WorkOrderTaskAsstDetailsClassName, into: DatabaseController.getContext()) as! AssetList

                    let woTaskAsstId = workOrderTaskAsstDetailsItem["woTaskAsstId"]! as? Int
                    if woTaskAsstId != nil {
                        workOrderTaskAsstDetails.woTaskAsstId = Int16(UInt16(woTaskAsstId!))
                    }

                    workOrderTaskAsstDetails.woTaskNo = workOrderTaskAsstDetailsItem.value(forKey: "woTaskNo") as? String
                    workOrderTaskAsstDetails.assetId = workOrderTaskAsstDetailsItem.value(forKey: "assetId") as? String
                    workOrderTaskAsstDetails.descripton = workOrderTaskAsstDetailsItem.value(forKey: "description") as? String
                    workOrderTaskAsstDetails.statusID = workOrderTaskAsstDetailsItem.value(forKey: "statusID") as? String
                    if workOrderTaskAsstDetailsItem["projectedStartDate"] != nil {
                        workOrderTaskAsstDetails.projectedStartDate = (workOrderTaskAsstDetailsItem["projectedStartDate"]! as? Double)!

                    }
                    if workOrderTaskAsstDetailsItem["actualStartDate"] != nil {
                        workOrderTaskAsstDetails.actualStartDate = (workOrderTaskAsstDetailsItem["actualStartDate"]! as? Double)!

                    }
                    if workOrderTaskAsstDetailsItem["projectedCompleteDate"] != nil {
                        workOrderTaskAsstDetails.projectedCompleteDate = (workOrderTaskAsstDetailsItem["projectedCompleteDate"]! as? Double)!

                    }
                    if workOrderTaskAsstDetailsItem["actualCompleteDate"] != nil {
                        workOrderTaskAsstDetails.actualCompleteDate = (workOrderTaskAsstDetailsItem["actualCompleteDate"]! as? Double)!

                    }
                    if workOrderTaskAsstDetailsItem["lastPaintedDate"] != nil {
                        workOrderTaskAsstDetails.lastPaintedDate = (workOrderTaskAsstDetailsItem["lastPaintedDate"]! as? Double)!

                    }
                    if workOrderTaskAsstDetailsItem["workOrderStartDate"] != nil {
                        workOrderTaskAsstDetails.workOrderStartDate = (workOrderTaskAsstDetailsItem["workOrderStartDate"]! as? Double)!

                    }
                    if workOrderTaskAsstDetailsItem["createDate"] != nil {
                        workOrderTaskAsstDetails.createDate = (workOrderTaskAsstDetailsItem["createDate"]! as? Double)!

                    }
                    if workOrderTaskAsstDetailsItem["updateDate"] != nil {
                        workOrderTaskAsstDetails.updateDate = (workOrderTaskAsstDetailsItem["updateDate"]! as? Double)!

                    }
                    workOrderTaskAsstDetails.workOrderNumber = workOrderTaskAsstDetailsItem.value(forKey: "workOrderNumber") as? String
                    workOrderTaskAsstDetails.beforePicLatitude = workOrderTaskAsstDetailsItem.value(forKey: "beforePicLatitude") as? String
                    workOrderTaskAsstDetails.beforePicLongitude = workOrderTaskAsstDetailsItem.value(forKey: "beforePicLongitude") as? String
                    workOrderTaskAsstDetails.afterPicLatitude = workOrderTaskAsstDetailsItem.value(forKey: "afterPicLatitude") as? String
                    workOrderTaskAsstDetails.afterPicLongitude = workOrderTaskAsstDetailsItem.value(forKey: "afterPicLongitude") as? String

                    let transactionType = workOrderTaskAsstDetailsItem["transactionType"]! as? Int
                    if transactionType != nil {
                        workOrderTaskAsstDetails.transactionType = Int16(UInt16(transactionType!))
                    }
                    let assetValid = workOrderTaskAsstDetailsItem["assetValid"]! as? Int
                    if assetValid != nil {
                        workOrderTaskAsstDetails.assetValid = Int16(UInt16(assetValid!))
                    }

                    //Asset
                    let AssetClassName:String = String(describing: AssetListAsset.self)
                    let asset:AssetListAsset = NSEntityDescription.insertNewObject(forEntityName: AssetClassName, into: DatabaseController.getContext()) as! AssetListAsset

                    let assetDict = workOrderTaskAsstDetailsItem["asset"]! as? NSDictionary
                    asset.descripton = assetDict?["description"]! as? String
                    asset.type = assetDict?["type"]! as? String
                    asset.assetId = assetDict?["assetId"]! as? String
                    if assetDict?["asset200ft"] != nil {
                        asset.asset200ft = assetDict?["asset200ft"]! as? String
                    }


                    if  assetDict?["streetNumber"] != nil{
                        asset.streetNumber = assetDict?["streetNumber"]! as? String
                    }

                    asset.county = assetDict?["county"]! as? String
                    asset.latitude = assetDict?["latitude"]! as? String
                    asset.longitude = assetDict?["longitude"]! as? String
                    workOrderTaskAsstDetails.assetListAsset = asset

                    //Condition

                    if workOrderTaskAsstDetailsItem["assetConditions"] != nil{
                        let conditionDict = workOrderTaskAsstDetailsItem["assetConditions"] as! [NSDictionary]

                        if conditionDict.count > 0{
                            let conditinSet = NSMutableSet()

                            for assetConditionsDetailsDict in conditionDict {

                                let ConditionClassName:String = String(describing: AssetListCondition.self)
                                let condition:AssetListCondition = NSEntityDescription.insertNewObject(forEntityName: ConditionClassName, into: DatabaseController.getContext()) as! AssetListCondition

                                let assetConditionItem = assetConditionsDetailsDict["condition"] as! NSDictionary
                                condition.id = assetConditionItem["id"]as? String
                                condition.label = assetConditionItem["label"]as? String
                                condition.rule = assetConditionItem["rule"]as? String
                                condition.descriptin = assetConditionItem["description"]as? String
                                conditinSet.add(condition)


                            }
                            workOrderTaskAsstDetails.addToAssetListCondition(conditinSet as NSSet)

                        }
                    }
                    set.add(workOrderTaskAsstDetails)



                }
                workOrderData.addToAssetList(set as NSSet)

            }



        }




        DatabaseController.saveContext()





    }
    fetchFrmDB()
}

代码包括其他表格,我还没有在图片中显示 请帮帮我..先谢谢

0 个答案:

没有答案