gpt4 book ai didi

arrays - 无法分配给属性 : 'itemArray' is a get-only property

转载 作者:行者123 更新时间:2023-11-30 10:54:04 27 4
gpt4 key购买 nike

我目前正在尝试使用 UISearchBar 和 Core Data 实现一个简单的搜索功能,我在此函数中收到错误“无法分配给属性:“ItemArray”是仅获取属性”如果有人可以帮助解决该错误并且还建议另一种简单的方法来实现搜索,这将非常有帮助。

func loadItems(with request: NSFetchRequest<Item>) {
let request : NSFetchRequest<Item> = Item.fetchRequest()

do {
itemArray = try context.fetch(request)
} catch {
print("Error fetching data from context")
}
tableView.reloadData()
}


}

这是整个文件:

import UIKit
import CoreData



class TodoListViewController: UITableViewController {

var itemArray: [Item] { return Array(self.category.items!) as! [Item] }


let context = (UIApplication.shared.delegate as! AppDelegate).persistentContainer.viewContext

var category: Category!


override func viewDidLoad() {
super.viewDidLoad()

print(FileManager.default.urls(for: .documentDirectory, in: .userDomainMask))


}

@IBOutlet weak var todoTableView: UITableView!

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

override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {

let cell = tableView.dequeueReusableCell(withIdentifier: "ToDoItemCell", for: indexPath)


let item = itemArray[indexPath.row]

cell.textLabel?.text = item.title

cell.accessoryType = item.done ? .checkmark : .none

return cell

}



override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
//print(itemArray[indexPath.row])

itemArray[indexPath.row].done = !itemArray[indexPath.row].done

saveItems()

tableView.deselectRow(at: indexPath, animated: true)

}


@IBAction func addButtonPressed(_ sender: UIBarButtonItem) {

var textField = UITextField()

let alert = UIAlertController(title: "Add New Todoey Item", message: "", preferredStyle: .alert)

let action = UIAlertAction(title: "Add Item", style: .default) { [weak self] (action) in
guard let self = self else { return }

let context = (UIApplication.shared.delegate as! AppDelegate).persistentContainer.viewContext
let newItem = Item(context: self.context)
newItem.title = textField.text!
self.category.addToItems(newItem)

self.saveItems()

}

alert.addTextField { (alertTextField) in
alertTextField.placeholder = "Create new item"
textField = alertTextField
print(alertTextField.text)
}


alert.addAction(action)

self.present(alert, animated: true, completion: nil)

}

func saveItems() {

do {
try context.save()
} catch {
print("Error saving context")
}

self.tableView.reloadData()
}

func loadItems(with request: NSFetchRequest<Item>) {
let request : NSFetchRequest<Item> = Item.fetchRequest()

do {
itemArray = try context.fetch(request)
} catch {
print("Error fetching data from context")
}
tableView.reloadData()
}


}

extension TodoListViewController: UISearchBarDelegate {
func searchBarSearchButtonClicked(_ searchBar: UISearchBar) {

let request : NSFetchRequest<Item> = Item.fetchRequest()
request.predicate = NSPredicate(format: "title CONTAINS[cd] %@", searchBar.text!)


request.sortDescriptors = [NSSortDescriptor(key: "title", ascending: true)]

loadItems(with: request)

tableView.reloadData()


}

最佳答案

itemArray被声明为只读属性,这正是错误所说的。

解决方案是将属性声明为空数组

var itemArray = [Item]()

并调用loadItemsviewDidLoad()

override func viewDidLoad() {
super.viewDidLoad()
loadItems()
print(FileManager.default.urls(for: .documentDirectory, in: .userDomainMask))
}

loadItems中的参数没有意义,省略。

func loadItems() { ... }

如果您想获取特定 Category 的项目添加适当的NSPredicateloadItems

关于arrays - 无法分配给属性 : 'itemArray' is a get-only property,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54154603/

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