Swift,此 class 与键 "array" 的键值编码不兼容

Swift, This class is not key value coding-compliant for the key "array"

我正在开发 popover 控制器,在打开 popover 时,我将数组(属性)传输到 popoverController (SortingPopoverController)。为了查看,我创建了 popView.xib 并且在 firstOwner 中,我附加了 "SortingPopovercontroller".

下面是我的代码

    func sortingPressed(sender: AnyObject){
            var sortingPopView = SortingPopoverController(nibName: "PopView",bundle: nil )

     var sortingPopoverController = UIPopoverController(contentViewController: sortingPopView)

      sortingPopoverController.popoverContentSize = CGSize(width: 250, height: 100)


      sortingPopoverController.presentPopoverFromBarButtonItem(sortingBtn, permittedArrowDirections: UIPopoverArrowDirection.Up
       , animated: true)

      sortingPopoverController.setValue(properties, forKey: "properties") //i am passing this array to the "sorting controller"

  }

///排序控制器代码

    class SortingPopoverController: UIViewController
        {
            var properties:[Property] = [Property]()
            var propertyNameSrt = false
            var addressSrt = false
            var ascSorting = false
            var utility = Utility()

             override func viewDidLoad()
                {
                     super.viewDidLoad()

                     let propertyNameSorting = UITapGestureRecognizer(target: self, action: "propertyNameSorting:")
                      self.propertyNameView.addGestureRecognizer(propertyNameSorting)

                     let addressSorting = UITapGestureRecognizer(target: self, action: "addressSorting:")
                     self.addressNameView.addGestureRecognizer(addressSorting)
                     imgTickPropertyName.hidden = true
                     imgTickAddress.hidden = true

                     properties = self.valueForKey("properties") as! [Property]
                     println(properties.count)

                }
        }

视图确实加载我收到错误消息,因为“此 class 与键属性 的键值编码不兼容”

sortingPopoverController 是一个 UIPopoverController。您打算使用 sortingPopView,而 SortingPopoverController.

sortingPopView.setValue(properties, forKey: "properties") //i am passing this array to the "sorting controller"

此处不需要 Key-Value Coding 语法。

sortingPopView.properties = properties

也会做同样的事情。注意:以上内容也是类型安全的,因此如果 sortingPopView.propertiesproperties 是不同的类型,您将收到警告或错误。