我需要將button
的frame
保存爲密鑰,將index
保存爲value
。下面是代碼:Swift:Dictionary Key as CGRect
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'
UPDATE
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] –