我目前正在尝试使用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()
}
答案 0 :(得分:0)
itemArray
被声明为只读属性,这正是错误所言。
一种解决方案是将属性声明为空数组
var itemArray = [Item]()
并在loadItems
中呼叫viewDidLoad()
override func viewDidLoad() {
super.viewDidLoad()
loadItems()
print(FileManager.default.urls(for: .documentDirectory, in: .userDomainMask))
}
loadItems
中的参数没有意义,请忽略它。
func loadItems() { ... }
如果要获取特定Category
的商品,请在NSPredicate
中添加适当的loadItems