【问题标题】:Swift, This class is not key value coding-compliant for the key "array"Swift,此类不符合键“数组”的键值编码
【发布时间】:2015-06-02 09:48:49
【问题描述】:

我正在开发 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)

                }
        }

在查看是否加载时出现错误,因为“此类不符合关键属性的关键值编码

【问题讨论】:

  • 哪一行报错
  • properties = self.valueForKey("properties") as! [Property] 的意图是什么?
  • @Memon...sortingPopoverController.setValue(properties, forKey: "properties") //我将此数组传递给“排序控制器”............ ..........line 出错
  • @JefferyThomas...它将获取“properties”的集合对象..我假设..........properties = self.valueForKey("properties") as! [属性]
  • self.valueForKey("properties") 返回与self.properties 相同的对象,所以你说的是properties = properties as! [Property]

标签: swift uipopovercontroller popover


【解决方案1】:

sortingPopoverControllerUIPopoverController。你的意思是使用sortingPopView,是SortingPopoverController

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

这里应该不需要Key-Value Coding 语法。

sortingPopView.properties = properties

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

【讨论】:

  • 太完美了!但我在数组中的计数为 0...在弹出控制器中..请告知?
  • 是不是因为我正在初始化数组,例如 (var properties:[Property] = [Property]())?
  • 无论如何......你已经回答了我的问题......所以你应该得到 +1..谢谢
  • @dhavalshah 我不知道为什么properties.count == 0。你确定properties.count > 0func sortingPressed() 中吗?
猜你喜欢
  • 1970-01-01
  • 2016-09-10
  • 2019-06-23
  • 2017-11-29
  • 2017-10-15
  • 1970-01-01
  • 2017-12-23
相关资源
最近更新 更多