类型“类别”不符合协议“序列”

时间:2019-07-07 17:21:52

标签: swift

我想从另一个带有协议功能的视图控制器中带入2个属性值,以将对象(Book)附加到我在主菜单中实例化的一系列Dictionary [Categories]中的Dictionary的值上

Category对象具有一个带有字典的构造函数:键名:String,值:[Books]

当我尝试使用协议功能执行此操作时,当我正在为“类型'类别'不符合协议'序列'”的词典时出现该错误

我真的不知道该怎么办。 我对此很陌生,请帮助! 谢谢!

// protocol to add new book
    protocol AddBookDelegate {
        func addNewBook(name: String, book: Book)
    }

// mainViewController + categories array + protocol func

class BooksTableViewController: UITableViewController, StockQuantityDelegate, AddBookDelegate{

    // categories array
    var todasLasCategorias: [Category] = []

    // protocol func to add a book
    func addNewBook(name: String, book: Book) {
            for eachCategory in todasLasCategorias{
                for (myKey, myValue) in eachCategory{
                    if myKey == name{
                        myValue.append(book)
                    }
                }
            }
        }

//Category declaration
class Category {
    private var name: String
    var books: [Book]?

    init(name:String){
        self.name = name
    }

    init(with dictionary: [String: AnyObject]){
        self.name = dictionary["name"] as? String ?? "no name"
        self.books = []
        if let apendeoLibros = dictionary["books"] as? [[String: AnyObject]]{
            for objectSelected in apendeoLibros{
                let newBook = Book(with: objectSelected)
                books?.append(newBook)
            }
        }
    }
}

0 个答案:

没有答案