2015-04-21 72 views
0

我正在爲其屬性創建出口ImageView集合和自定義類「TTTImageView」。我想補充TapGestureRecognizer所有的ImageView,但我發現這個錯誤在swift中運行此代碼時出現類型不匹配錯誤

「的NSArray元素眼福雨燕數組元素類型」

我ViewController.swift類是這個樣子:

import UIKit 

class ViewController: UIViewController { 



@IBOutlet var fields: [TTTImageView]! 

override func viewDidLoad() { 
    super.viewDidLoad() 
    self.setUpLogic() 
      // Do any additional setup after loading the view, typically from a nib. 
} 

func setUpLogic(){ 
    for index in 0 ... fields.count-1{ 
     let gestureRecognizer = UITapGestureRecognizer(target: self, action: "fieldTapped") 
     gestureRecognizer.numberOfTapsRequired = 1 
     fields[index].addGestureRecognizer(gestureRecognizer) 
    } 
} 

func fieldTapped(recognizer:UITapGestureRecognizer){ 

    var img = recognizer.view as TTTImageView 
    img.setPlayer("x") 


} 
override func didReceiveMemoryWarning() { 
    super.didReceiveMemoryWarning() 
    // Dispose of any resources that can be recreated. 
} 

}

和TTTImageView.swift是:

import UIKit 

class TTTImageView: UIImageView { 
var player : String = String() 

func setPlayer(player : String){ 
    self.player = player 
    if(player == "x"){ 
     self.image = UIImage(named: "alphabet X") 
    } 
    else{ 
     self.image = UIImage(named: "alphabet O") 
    } 
} 

} 

我認爲這是一個類型轉換問題,但不知道如何解決。請讓我解決這個問題。由於

+1

發生什麼錯誤? –

+0

就此線路。 fields [index] .addGestureRecognizer(gestureRecognizer) –

+0

你做了一個乾淨的構建?一見鍾情似乎不對。 – vrwim

回答

0

也許你想這是什麼

@IBOutlet var fields: [TTTImageView!] 

,而不是這個

@IBOutlet var fields: [TTTImageView]! 

請注意!字符。

相關問題