在这段代码中,我试图更新对象,但我在更新列表数组时遇到了麻烦,导致了意外的结果,特别是在应用editTask脚本时,似乎他们将新对象添加到Realm中,但没有更新数据。我不知道我的代码的哪一部分是错误的,请帮助我在Realm中修复此editTask。
这是我的领域模型:
class DatabaseTask {
static let shared = DatabaseTask()
var realm = try! Realm()
func createTask(title: String, date: String, startTime: String, endTime: String, users: [User], category: [Categories], status: [Status]) {
try! realm.write {
let task = Task()
task.title = title
task.date = date
task.startTime = startTime
task.endTime = endTime
let listUser = List<User>()
listUser.append(objectsIn: users)
task.user = listUser
let listCategory = List<Categories>()
listCategory.append(objectsIn: category)
task.category = listCategory
let listStatus = List<Status>()
listStatus.append(objectsIn: status)
task.status = listStatus
realm.add(task)
}
}
func editTask(task: Task?, newTitle: String, newDate: String, newStartTime: String, newEndTime: String , newUser: [User], newCategory: [Categories]) {
try! realm.write {
task?.title = newTitle
task?.date = newDate
task?.startTime = newStartTime
task?.endTime = newEndTime
task?.user.removeAll()
task?.user.append(objectsIn: newUser)
task?.category.removeAll()
task?.category.append(objectsIn: newCategory)
}
}
这是处理创建和编辑的屏幕:我在taskEdit变量中存储来自上一个屏幕的数据,现在当我有了来自刚刚创建域的屏幕的数据时,我使用数据taskEdit再次编辑数据域
import UIKit
class TaskCalenderVC: UIViewController {
@IBOutlet weak var myTable: UITableView!
@IBOutlet weak var btMore: UIButton!
@IBOutlet weak var createTask: UIButton!
var arrUsers: [User] = []
var arrCategori: [Categories] = []
var arrStatus: [Status] = []
var titleTask: String = ""
var dateTask : String = ""
var startTimes: String = ""
var endTimes : String = ""
var taskEdit: Task?
override func viewDidLoad() {
super.viewDidLoad()
myTable.dataSource = self
myTable.delegate = self
}
@IBAction func createTasks(_ sender: Any) {
let saveTasks = DatabaseTask.shared.getTask()
let titleOverlapping = saveTasks.contains(where: { $0.title == titleTask })
let dateOverlapping = saveTasks.contains(where: { $0.date == dateTask })
let startTimeOverlapping = saveTasks.contains(where: { $0.startTime == startTimes })
let endTimeOverlapping = saveTasks.contains(where: { $0.endTime == endTimes })
let usersOverlapping = saveTasks.contains(where: { $0.user.toArray().contains(where: { arrUsers.contains($0) }) })
if titleOverlapping || dateOverlapping || startTimeOverlapping || endTimeOverlapping || usersOverlapping {
showAlert(with: "Error", message: "Task values overlap or are empty.")
} else {
if let taskEdit = taskEdit {
// Edit task
DatabaseTask.shared.editTask(task: taskEdit, newTitle: titleTask, newDate: dateTask, newStartTime: startTimes, newEndTime: endTimes, newUser: arrUsers, newCategory: arrCategori)
} else {
// Create task
DatabaseTask.shared.createTask(title: titleTask, date: dateTask, startTime: startTimes, endTime: endTimes, users: arrUsers, category: arrCategori, status: arrStatus)
}
self.dismiss(animated: true)
self.tabBarController?.selectedIndex = 0
}
}
func showAlert(with title: String, message: String) {
let alert = UIAlertController(title: title, message: message, preferredStyle: .alert)
alert.addAction(UIAlertAction(title: "OK", style: .default, handler: nil))
present(alert, animated: true, completion: nil)
}
}
1条答案
按热度按时间ikfrs5lh1#
和editTask函数错误在境界模型我找到了解决方案来修复它,为什么removeAll()函数它真的不工作与列表我认为是这样的.下面是removeAll()函数的替代方法。
另外,当你想把list转换成一个对象数组时,添加两个扩展。现在list已经是一个对象数组了。编码现在容易多了