Ios 类型为'的值;项目';没有成员';父类别';

Ios 类型为'的值;项目';没有成员';父类别';,ios,swift,xcode,core-data,Ios,Swift,Xcode,Core Data,我有两个实体 我现在正在使用XCode 10,我不确定我是做错了什么还是XCode bug 我在这些行中添加了1行 let newItem = Item(context: self.context) newItem.title = textField.text! newItem.done = false newItem.parentCategory = self.selectedCategory <--------- ADD HERE self.itemArray.append(new

我有两个实体

我现在正在使用XCode 10,我不确定我是做错了什么还是XCode bug

我在这些行中添加了1行

let newItem = Item(context: self.context)
newItem.title = textField.text!
newItem.done = false
newItem.parentCategory = self.selectedCategory <--------- ADD HERE 
self.itemArray.append(newItem)
self.saveItems()
let newItem=Item(上下文:self.context)
newItem.title=textField.text!
newItem.done=false
newItem.parentCategory=self.selectedCategory Int{
返回itemArray.count
}
重写func tableView(tableView:UITableView,cellForRowAt indexath:indexPath)->UITableViewCell{
let cell=tableView.dequeueReusableCell(标识符为:“listItemCell”,表示:indexPath)
let item=itemArray[indexPath.row]
cell.textLabel?.text=item.title
cell.accessoryType=item.done==true?。选中标记:。无
返回单元
}
// ---------------------------------------------------------------------------------------------------------
//标记-代表
重写func tableView(tableView:UITableView,didSelectRowAt indexPath:indexPath){
delete(itemArray[indexath.row])
itemArray.remove(位于:indexPath.row)
itemArray[indexPath.row].done=!itemArray[indexPath.row].done
self.saveItems()
取消选择行(at:indexPath,动画:true)
}
// ---------------------------------------------------------------------------------------------------------
//标记-添加新项目
@iAction func addButton已按下(u发件人:UIBarButtonim){
var textField=UITextField()
let alert=UIAlertController(标题:“添加新项目”,消息:,首选样式:。警报)
//行动
让action=UIAlertAction(标题:“添加项”,样式:。默认值){(操作)在
让newItem=Item(上下文:self.context)
newItem.title=textField.text!
newItem.done=false
newItem.parentCategory=self.selectedCategory
self.itemArray.append(newItem)
self.saveItems()
}
alert.addTextField{(alertTextField)位于
alertTextField.placeholder=“创建新项目”
textField=alertTextField
}
alert.addAction(操作)
当前(警报、动画:真、完成:无)
}
// ---------------------------------------------------------------------------------------------------------
//标记模型操作方法
func saveItems(){
做{
尝试context.save()
}抓住{
打印(“保存上下文时出错,\(错误)”)
}
self.tableView.reloadData()
}
func loadItems(请求为:NSFetchRequest=Item.fetchRequest()){
let predicate=NSPredicate(格式:“parentCategory.name匹配%@”,selectedCategory?.name!)
request.predicate=谓词
做{
itemArray=try context.fetch(请求)
}抓住{
打印(“从上下文提取数据时出错,\(错误)”)
}
self.tableView.reloadData()
}
}
//标记:-搜索栏方法
对olistvc的扩展:UISearchBarDelegate{
func searchBar searchbutton单击(searchBar:UISearchBar){
let请求:NSFetchRequest=Item.fetchRequest()
request.predicate=NSPredicate(格式:“title包含[cd]]@”,searchBar.text!)
request.sortDescriptors=[NSSortDescriptor(键:“title”,升序:true)]
loadItems(带:请求)
}
func searchBar(searchBar:UISearchBar,textDidChange searchText:String){
如果searchBar.text?.count==0{
loadItems()
DispatchQueue.main.async{
searchBar.resignFirstResponder()辞职
}
}
}
}
类别
//
//分类
//利斯休
//版权所有©2018 LR网页设计。版权所有。
//
导入UIKit
导入CoreData
类类别VC:UITableViewController{
变量类别=[类别]()
让上下文=(UIApplication.shared.delegate为!AppDelegate)。persistentContainer.viewContext
重写func viewDidLoad(){
super.viewDidLoad()
装载类别()
}
// ---------------------------------------------------------------------------------------------------------
//标记-表视图数据源
重写func tableView(tableView:UITableView,numberofrowsinssection:Int)->Int{
返回类别。计数
}
重写func tableView(tableView:UITableView,cellForRowAt indexath:indexPath)->UITableViewCell{
let cell=tableView.dequeueReusableCell(标识符为“CategoryCell”,表示:indexath)
让category=categories[indexPath.row]
cell.textLabel?.text=category.name
返回单元
}
// ---------------------------------------------------------------------------------------------------------
//标记-表视图委托方法
重写func tableView(tableView:UITableView,didSelectRowAt indexPath:indexPath){
取消选择行(at:indexPath,动画:true)
性能检查(带有标识符:“goToItems”,发送者:self)
}
覆盖功能准备(对于segue:UIStoryboardSegue,发送方:有吗?){
让destinationVC=segue.destination为!TodoListVC
//获取所选单元格的类别
如果让indexPath=tableView.indexPathForSelectedRow{
//设置属性
destinationVC.selectedCategory=categories[indexPath.row]
}
}
// --------
//
//  TodoListVC
//  ListHue
//  Copyright © 2018 LR Web Design. All rights reserved.
//

import UIKit
import CoreData

class TodoListVC: UITableViewController {

    var itemArray = [Item]()
    
    var selectedCategory : Category? {
        didSet {
            loadItems()
        }
    }
    
    let context = (UIApplication.shared.delegate as! AppDelegate).persistentContainer.viewContext
    
    // ---------------------------------------------------------------------------------------------------------
    //MARK - viewDidLoad
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        print(FileManager.default.urls(for: .documentDirectory, in: .userDomainMask))
        
        
    }
    

    // ---------------------------------------------------------------------------------------------------------
    //MARK - Datasource
    
    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: "listItemCell", for: indexPath)
        let item = itemArray[indexPath.row]
        
        cell.textLabel?.text = item.title
        cell.accessoryType = item.done == true ? .checkmark : .none
        
        return cell
        
    }
    
    
    // ---------------------------------------------------------------------------------------------------------
    //MARK - Delegate
    
    override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        
        
        context.delete(itemArray[indexPath.row])
        itemArray.remove(at: indexPath.row)
        
        
        itemArray[indexPath.row].done = !itemArray[indexPath.row].done
        
        self.saveItems()
        
        tableView.deselectRow(at: indexPath, animated: true)
    }
    

    // ---------------------------------------------------------------------------------------------------------
    //MARK - Add new item
    
    @IBAction func addButtonPressed(_ sender: UIBarButtonItem) {
       
        var textField = UITextField()
        let alert = UIAlertController(title: "Add New Item", message: "", preferredStyle: .alert)
        
        //action
        let action = UIAlertAction(title: "Add Item", style: .default) { (action) in
    
            
            let newItem = Item(context: self.context)
            newItem.title = textField.text!
            newItem.done = false
            newItem.parentCategory = self.selectedCategory
            self.itemArray.append(newItem)
            self.saveItems()
           
            
        }
        
        alert.addTextField { (alertTextField) in
            alertTextField.placeholder = "Create new item"
            textField = alertTextField
        }
        
        alert.addAction(action)
        
        present(alert, animated: true, completion: nil)
        
    }
    
    // ---------------------------------------------------------------------------------------------------------
    //MARK - Model Manipulation Methods
    
    
    func saveItems() {
        
        do {
            try context.save()
        } catch {
            print("Error saving context, \(error)")
        }
        
        self.tableView.reloadData()
        
    }

    func loadItems(with request: NSFetchRequest<Item> = Item.fetchRequest()) {
        
        let predicate = NSPredicate(format: "parentCategory.name MATCHES %@", selectedCategory?.name!)
        request.predicate = predicate

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

}

//MARK: - Search bar methods

extension TodoListVC : 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)
    }
    
    func searchBar(_ searchBar: UISearchBar, textDidChange searchText: String) {
        if searchBar.text?.count == 0 {
            
            loadItems()
            
            DispatchQueue.main.async {
                searchBar.resignFirstResponder()
            }
            
        }
    }
    
    
}
//
//  CategoryVC.swift
//  ListHue
//  Copyright © 2018 LR Web Design. All rights reserved.
//

import UIKit
import CoreData

class CategoryVC: UITableViewController {
    
    var categories = [Category]()
    let context = (UIApplication.shared.delegate as! AppDelegate).persistentContainer.viewContext

    override func viewDidLoad() {
        super.viewDidLoad()
        
        loadCategories()

    }
    
    // ---------------------------------------------------------------------------------------------------------
    //MARK - Table View Datasource
    
    override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return categories.count
    }
    
    override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        
        let cell = tableView.dequeueReusableCell(withIdentifier: "CategoryCell", for: indexPath)
        let category = categories[indexPath.row]

        cell.textLabel?.text = category.name

        
        return cell
        
    }
    
    
    // ---------------------------------------------------------------------------------------------------------
    //MARK - Table View Delegate Methods
    
    override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        
        tableView.deselectRow(at: indexPath, animated: true)
        performSegue(withIdentifier: "goToItems", sender: self)
    
    }
    
    
    override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
        
        let destinationVC = segue.destination as! TodoListVC
        
        //get the category of the selected cell
        if let indexPath = tableView.indexPathForSelectedRow {
            
            //set the property
            destinationVC.selectedCategory = categories[indexPath.row]
            
        }
        
    }
    

    // ---------------------------------------------------------------------------------------------------------
    //MARK - Data Manipulation Methods
    
    @IBAction func addButtonPressed(_ sender: UIBarButtonItem) {
        print("click")
        
        var textField = UITextField()
        let alert = UIAlertController(title: "Add New Category", message: "", preferredStyle: .alert)
        
        //action
        let action = UIAlertAction(title: "Add Category", style: .default) { (action) in
            
            let newCategory = Category(context: self.context)
            newCategory.name = textField.text!
            self.categories.append(newCategory)
            self.saveCategories()
        }
        
        alert.addTextField { (alertTextField) in
            alertTextField.placeholder = "Create new item"
            textField = alertTextField
        }
        
        alert.addAction(action)
        
        present(alert, animated: true, completion: nil)
        
        
    }
    
    // ---------------------------------------------------------------------------------------------------------
    //MARK - Add New Categories
    func saveCategories() {
        
        do {
            try context.save()
        } catch {
            print("Error saving context, \(error)")
        }
        
        self.tableView.reloadData()
        
    }
    
    func loadCategories(with request: NSFetchRequest<Category> = Category.fetchRequest()) {
        
        do {
            categories = try context.fetch(request)
        } catch {
            print("Error fetching data from the context, \(error)")
        }
        
        self.tableView.reloadData()
        
    }
    
}