【发布时间】:2015-07-10 23:31:50
【问题描述】:
我需要将button 的frame 保存为键,并将数组中的index 保存为其value。下面是代码:
var buttonLocationWithIndex = Dictionary<CGRect, Int>()
override func viewDidAppear(animated: Bool) {
super.viewDidAppear(animated)
for (index, point) in enumerate(self.points){
let pointButton = UIButton()
pointButton.frame = point.bounds
self.buttonLocationWithIndex[point.frame] = index
self.view.addSubview(pointButton)
}
我在尝试声明字典时遇到错误:Type 'CGRect' does not confirm to protocol 'Hashable'
更新
func pressed(sender: UIButton!) {
var alertView = UIAlertView();
alertView.addButtonWithTitle("Ok");
alertView.title = "title";
var boundsIndex = self.buttonLocationWithIndex[sender.frame]
var value = self.points(boundsIndex)
alertView.message = value
alertView.show();
}
错误: Cannot invoke points with an arguments list of type (Int?)'
【问题讨论】:
-
self.points(boundsIndex) 需要为 self.points[boundsIndex]
标签: swift nsdictionary