我有一个包含 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()
Error: "Variable activityIndicator used before initialized
同样
activityIndicator.stopAnimating();
在回调中我得到了错误:
"Variable previewViewImage captured by a closure before being initialized"
我是 Swift 的新手,不明白为什么我的代码无法运行。谁能帮我重构上面的代码?
最佳答案
Swift 看到一个可能的路径,其中 previewViewImage
和 activityIndicator
没有被初始化。这是您的代码:
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;
}
如果 controlIndex
为 5
会发生什么?两个变量都不会被初始化。因此,Swift 将它们视为可能未初始化,这就是您收到错误的原因。
您可以通过将最后一个 else if
简单地设为 else
来解决此问题。在这种情况下,您需要断言
controlIndex == 4
。或者,您可以在 if
之前将 previewViewImage
和 activityIndicator
初始化为一些合理的默认值。
关于ios - 变量初始化错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32024604/