核心数据 - 一对多关系变为零

时间:2015-08-19 23:36:45

标签: ios swift core-data relationship one-to-many

有人能帮助我吗? 为什么这不起作用?

func appendTrainingDay() {
        let dayLabel = dayName.text
        let dayEntity = NSEntityDescription.entityForName("TrainingDay", inManagedObjectContext: moc!)
        let newTrainingDay = TrainingDay(entity: dayEntity!, insertIntoManagedObjectContext: moc)
        newTrainingDay.day = dayName.text

        var error: NSError?
        moc?.save(&error)

        if let err = error {
            var status = err.localizedFailureReason
            println("\(status)")
        } else {
            println("Training day:\(dayName.text)")
    }
}

- >节省一天

func appendTrainingDetailsToArray () {
        let nameLabel = exerciseName.text
        namesArray.append(nameLabel)
        let numberOfSets = setsNumber.text?.toInt()
        setsArray.append(numberOfSets!)
        let numberOfReps = repsNumber.text?.toInt()
        repsArray.append(numberOfReps!)


    let detailsEntity = NSEntityDescription.entityForName("TrainingDetails", inManagedObjectContext: moc!)
    let trainingdetails = TrainingDetails(entity: detailsEntity!, insertIntoManagedObjectContext: moc)
    trainingdetails.exerciseName = exerciseName.text
    trainingdetails.setsNumber = setsNumber.text!
    trainingdetails.repsNumber = repsNumber.text!

    let fetchRequest = NSFetchRequest(entityName: "TrainingDay")
    if let fetchResults = moc!.executeFetchRequest(fetchRequest, error: nil) as? [TrainingDay] {
        daysArray = fetchResults
            let dayLabel = dayName.text
                for day in daysArray {
                    if day == dayLabel {
                    trainingdetails.trainingDay = day
            }
        }
    }

    var error: NSError?
    moc?.save(&error)

    if let err = error {
        var status = err.localizedFailureReason
        println("\(status)")
    } else {
        println("Exercise: #\(exerciseName.text) saved successfully!")
        println("Number of sets: #\(setsNumber.text) saved successfully!")
        println("Number of reps: #\(repsNumber.text) saved successfully!")
    }

    }

- >保存exerciseName,setsNumber和repsNumber

现在,这部分在这里:

let fetchRequest = NSFetchRequest(entityName: "TrainingDay")
        if let fetchResults = moc!.executeFetchRequest(fetchRequest, error: nil) as? [TrainingDay] {
            daysArray = fetchResults
                let dayLabel = dayName.text
                    for day in daysArray {
                        if day == dayLabel {
                        trainingdetails.trainingDay = day
                }
            }
        }

为什么呢?它类似于:“取一个东西。将此fetch的值分配给daysArray。 dayLabel接收用户打印的内容。拿这个名为“day”的变量,遍历daysArray,如果daysArray中的任何内容等于dayLabel,这是用户打印的内容,将其添加到trainingDay关系中。

你知道:

import Foundation
import CoreData

class TrainingDay: NSManagedObject {

    @NSManaged var day: String
    @NSManaged var trainingDetails: NSSet

}

import Foundation
import CoreData

class TrainingDetails: NSManagedObject {

    @NSManaged var exerciseName: String
    @NSManaged var repsNumber: String
    @NSManaged var setsNumber: String
    @NSManaged var trainingDay: TrainingDay

}

当我运行应用程序时,会发生这种情况:

(entity:TrainingDetails; id:0xd000000000040002; data:{     exerciseName =“exercise 1”;     repsNumber = 5;     setsNumber = 2;     trainingDay = nil;

trainingDay 仍然是零!我该怎么办?

编辑1:

代码:

import UIKit
import CoreData

class ExerciseViewController: UIViewController, UITableViewDelegate, UITableViewDataSource {

    override func viewDidLoad() {
        super.viewDidLoad()

        //sets stepper configs
        setsStepper.wraps = false
        setsStepper.autorepeat = true
        setsStepper.continuous = true
        setsStepper.tintColor = UIColor.redColor()
        setsStepper.minimumValue = 0
        setsStepper.maximumValue = 500
        setsStepper.value = 0

        //reps stepper configs
        repsStepper.wraps = false
        repsStepper.autorepeat = true
        repsStepper.continuous = true
        repsStepper.tintColor = UIColor.orangeColor()
        repsStepper.minimumValue = 0
        repsStepper.maximumValue = 500
        repsStepper.value = 0

        exerciseTableView.reloadData()
    }

    var moc = (UIApplication.sharedApplication().delegate as! AppDelegate).managedObjectContext

    var fetchedResultsController: NSFetchedResultsController?

    @IBOutlet var exerciseTableView: UITableView!

    @IBOutlet var dayName: UITextField!
    @IBOutlet var exerciseName: UITextField!
    @IBOutlet var setsStepper: UIStepper!
    @IBOutlet var repsStepper: UIStepper!

    @IBOutlet var setsNumber: UILabel!
    @IBOutlet var repsNumber: UILabel!

    var daysArray = [TrainingDay]()
    var namesArray = [String]()
    var setsArray = [Int]()
    var repsArray = [Int]()


    func appendTrainingDay() {
        let dayLabel = dayName.text
        let dayEntity = NSEntityDescription.entityForName("TrainingDay", inManagedObjectContext: moc!)
        let newTrainingDay = TrainingDay(entity: dayEntity!, insertIntoManagedObjectContext: moc)
        newTrainingDay.day = dayName.text

        var error: NSError?
        moc?.save(&error)

        if let err = error {
            var status = err.localizedFailureReason
            println("\(status)")
        } else {
            println("Training day:\(dayName.text)")
    }
}

    func appendTrainingDetailsToArray () {
        let nameLabel = exerciseName.text
        namesArray.append(nameLabel)
        let numberOfSets = setsNumber.text?.toInt()
        setsArray.append(numberOfSets!)
        let numberOfReps = repsNumber.text?.toInt()
        repsArray.append(numberOfReps!)


        let detailsEntity = NSEntityDescription.entityForName("TrainingDetails", inManagedObjectContext: moc!)
        let trainingdetails = TrainingDetails(entity: detailsEntity!, insertIntoManagedObjectContext: moc)
        trainingdetails.exerciseName = exerciseName.text
        trainingdetails.setsNumber = setsNumber.text!
        trainingdetails.repsNumber = repsNumber.text!

        let fetchRequest = NSFetchRequest(entityName: "TrainingDay")
        daysArray = (moc!.executeFetchRequest(fetchRequest, error: nil) as? [TrainingDay])!

        for chosenDay in daysArray {
            println(chosenDay.day)
            if chosenDay == dayName.text {
                println("CHOSEN DAY IS:\(chosenDay)")
                trainingdetails.trainingDay = chosenDay
            }
        }

        var error: NSError?
        moc?.save(&error)

        if let err = error {
            var status = err.localizedFailureReason
            println("\(status)")
        } else {
            println("Exercise: #\(exerciseName.text) saved successfully!")
            println("Number of sets: #\(setsNumber.text) saved successfully!")
            println("Number of reps: #\(repsNumber.text) saved successfully!")
        }
    }

    @IBAction func fixDayButton(sender: AnyObject) {
        appendTrainingDay()
    }


    @IBAction func doneButton(sender: AnyObject) {
        appendTrainingDetailsToArray()
        exerciseTableView.reloadData()
    }

    @IBAction func setsStepperAction(sender: UIStepper) {
        println("\(Int(sender.value))")
        setsNumber.text = Int(sender.value).description
    }

    @IBAction func repsStepper(sender: UIStepper) {
        println("\(Int(sender.value))")
        repsNumber.text = Int(sender.value).description
    }

    func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return namesArray.count
    }

    func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {

        let cellIdentifier = "exerciseCell"
var cell  = tableView.dequeueReusableCellWithIdentifier(cellIdentifier) as? UITableViewCell
        if cell == nil {
            cell = UITableViewCell(style: UITableViewCellStyle.Value2, reuseIdentifier: cellIdentifier)
        }
        let row = indexPath.row
        let name = namesArray[indexPath.row]
        let numberReps = repsArray[indexPath.row]
        let numberSets = setsArray[indexPath.row]
            cell!.textLabel!.text = name
        cell?.detailTextLabel?.text = "Sets: #\(numberSets) Reps: #\(numberReps)"
        return cell!
    }
}

1 个答案:

答案 0 :(得分:1)

if chosenDay.day == dayName.text

应改为

    var query = PFUser.query()
    userQuery?.whereKey("username", equalTo: "Ethan")

    userQuery?.getFirstObjectInBackgroundWithBlock({ (result, error) -> Void in
        if let thisUser = result as? PFUser {

            println(thisUser.username)

        }

    })