2015-08-15 248 views
0

我有一個包含4個單元格的表(縮略圖要從URI中顯示)。 如果URI爲空,我想顯示一個佔位符圖像。 如果不爲空,我想在下載時顯示一個活動指示器。變量初始化錯誤

什麼我是一個非常「快速和骯髒」的代碼 - 但它的工作原理:

func setCell(previewView1: String, previewView2: String, previewView3: String, previewView4: String, id: String) { 
    self.loadPreview1(previewView1) 
    self.loadPreview2(previewView2) 
    self.loadPreview3(previewView3) 
    self.loadPreview4(previewView4) 
    self.cellID = id; 
} 

func loadPreview1(urlString: String) { 
    if urlString == "" { 
     self.previewView1.image = UIImage(named: "imagePlatzhalter") 
     self.activityIndicator1.stopAnimating(); // Animation stoppen 
    } 
    else { 
     self.activityIndicator1.startAnimating() // Animation Start 
     var imgURL = NSURL(string: urlString); 
     let request: NSURLRequest = NSURLRequest(URL: imgURL!); 
     let mainQueue = NSOperationQueue.mainQueue(); 
     NSURLConnection.sendAsynchronousRequest(request, queue: mainQueue, completionHandler: { (response, data, error) -> Void in 
      if error == nil { 
       // Convert the downloaded data in to a UIImage object 
       let image = UIImage(data: data) 
       // Update the cell 
       self.activityIndicator1.stopAnimating(); // Animation stoppen 
       self.previewView1.image = image; 
      } 
      else { 
       println("Error: \(error.localizedDescription)") 
       self.previewView1.image = UIImage(named: "imagePlatzhalter") 
       self.activityIndicator1.stopAnimating(); // Animation stoppen 
      } 
     }) 
    } 
} 

func loadPreview2(urlString: String) { 
    if urlString == "" { 
     self.previewView2.image = UIImage(named: "imagePlatzhalter") 
     self.activityIndicator2.stopAnimating(); // Animation stoppen 
    } 
    else { 
     self.activityIndicator2.startAnimating() // Animation Start 
     var imgURL = NSURL(string: urlString); 
     let request: NSURLRequest = NSURLRequest(URL: imgURL!); 
     let mainQueue = NSOperationQueue.mainQueue(); 
     NSURLConnection.sendAsynchronousRequest(request, queue: mainQueue, completionHandler: { (response, data, error) -> Void in 
      if error == nil { 
       // Convert the downloaded data in to a UIImage object 
       let image = UIImage(data: data) 
       // Update the cell 
       self.activityIndicator2.stopAnimating(); // Animation stoppen 
       self.previewView2.image = image; 
      } 
      else { 
       println("Error: \(error.localizedDescription)") 
       self.previewView2.image = UIImage(named: "imagePlatzhalter") 
       self.activityIndicator2.stopAnimating(); // Animation stoppen 
      } 
     }) 
    } 
} 
func loadPreview3(urlString: String) { 
: same as 1 and 2 with references on self.previewView3 and self.activityIndicator3... 
} 

func loadPreview4(urlString: String) { 
: same as 1 and 2 with references on self.previewView4 and self.activityIndicator4... 
} 

這個解決方案工作正常,但現在我要重構代碼,在一個更好的解決方案。這是我的方法:

func previewImage (urlString: String, controlIndex: Int) { 
    var previewViewImage : UIImage; 
    var activityIndicator : UIActivityIndicatorView; 

    if controlIndex == 1 { 
     previewViewImage = self.previewView1.image!; 
     activityIndicator = self.activityIndicator1; 
    } else if controlIndex == 2 { 
     previewViewImage = self.previewView2.image!; 
     activityIndicator = self.activityIndicator2; 
    } else if controlIndex == 3 { 
     previewViewImage = self.previewView3.image!; 
     activityIndicator = self.activityIndicator3; 
    } else if controlIndex == 4 { 
     previewViewImage = self.previewView4.image!; 
     activityIndicator = self.activityIndicator4; 
    } 

    if urlString == "" { 
     // Set image to placeholder image: 
     previewViewImage = UIImage(named: "imagePlatzhalter")!; 
    } 
    else { 
     activityIndicator.startAnimating() // Animation Start 
     var imgURL = NSURL(string: urlString); 

     // Check ob Image gecacht ist/TODO 
     let request: NSURLRequest = NSURLRequest(URL: imgURL!); 
     let mainQueue = NSOperationQueue.mainQueue(); 
     NSURLConnection.sendAsynchronousRequest(request, queue: mainQueue, completionHandler: { (response, data, error) -> Void in 
      if error == nil { 
       // Convert the downloaded data in to a UIImage object 
       let image = UIImage(data: data) 
       // Store the image in to our cache 
       //self.imageCache[urlString] = image 

       // Update the cell 
       previewViewImage = image!; 
      } 
      else { 
       println("Error: \(error.localizedDescription)") 

       previewViewImage = UIImage(named: "imagePlatzhalter")!; 
      } 
     }) 

    } 
    // Stop activity indicator: 
    activityIndicator.stopAnimating(); 
} 

但Xcode的位置拋出3個錯誤:在

activityIndicator.stopAnimating(); 

activityIndicator.startAnimating() 

Error: "Variable activityIndicator used before initialized

相同,回調中我得到了錯誤:

"Variable previewViewImage captured by a closure before being initialized"

我是新來的斯威夫特,不明白,爲什麼我的代碼將無法正常工作。任何人都可以幫助我重構上面的代碼嗎?

回答

1

Swift發現一條可能的路徑,其中previewViewImageactivityIndicator未初始化。這裏是你的代碼:如果controlIndex5

func previewImage (urlString: String, controlIndex: Int) { 
    var previewViewImage : UIImage; 
    var activityIndicator : UIActivityIndicatorView; 

    if controlIndex == 1 { 
     previewViewImage = self.previewView1.image!; 
     activityIndicator = self.activityIndicator1; 
    } else if controlIndex == 2 { 
     previewViewImage = self.previewView2.image!; 
     activityIndicator = self.activityIndicator2; 
    } else if controlIndex == 3 { 
     previewViewImage = self.previewView3.image!; 
     activityIndicator = self.activityIndicator3; 
    } else if controlIndex == 4 { 
     previewViewImage = self.previewView4.image!; 
     activityIndicator = self.activityIndicator4; 
    } 

會發生什麼?這兩個變量都不會被初始化。所以,Swift把它們視爲未初始化的,這就是爲什麼你會得到錯誤。

您可以通過使最後的else if只是一個else來解決此問題。那樣的話,你想assertcontrolIndex == 4。或者,您可以在if之前將previewViewImageactivityIndicator初始化爲一些合理的默認值。

+0

該死的感謝了很多。改變最後的其他人,如果如果作品像魅力。到目前爲止,不知道Xcode/Swift是如此嚴格... –

+0

Swift只是幫你一個忙。它通過堅持已經正確初始化變量來消除很難找到錯誤的可能性。 – vacawama

+0

這是Swift風格,在行末尾處留下分號。 – vacawama