gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-29 01:44:08 25 4
gpt4 key购买 nike

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

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
}
}
}

为什么?就像是:“获取。将此提取的值分配给 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

}

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

(实体:TrainingDetails;id:0xd000000000040002;数据:{ 练习名 = "练习 1"; 代表数 = 5; 集数 = 2; 培训日=无;

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!
}
}

最佳答案

if chosenDay == dayName.text

应该改为

if chosenDay.day == dayName.text

关于ios - 核心数据 - 一对多关系变得零,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32107329/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com