变量初始化错误

时间:2015-08-15 12:13:32

标签: ios swift

我有一个包含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.startAnimating()
  

错误:“初始化之前使用的变量activityIndi​​cator

相同

activityIndicator.stopAnimating();

在回调中我得到了错误:

  

“变量previewViewImage由一个闭包在被捕获之前捕获   初始化“

我是Swift的新手并且不明白,为什么我的代码不起作用。任何人都可以帮我重构上面的代码吗?

1 个答案:

答案 0 :(得分:1)

Swift看到了previewViewImageactivityIndicator未初始化的可能路径。这是你的代码:

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;
    }

如果controlIndex5会怎样?这两个变量都不会被初始化。所以,Swift认为它们可能是未初始化的,这就是你得到错误的原因。

您可以通过将最后一个else if简单地设为else来解决此问题。在这种情况下,您希望assert controlIndex == 4。或者,您可以在previewViewImage之前将activityIndicatorif初始化为一些合理的默认值。

相关问题