变量初始化错误
问题描述:
我有一个包含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"
我是新来的斯威夫特,不明白,为什么我的代码将无法正常工作。任何人都可以帮助我重构上面的代码吗?
答
Swift发现一条可能的路径,其中previewViewImage
和activityIndicator
未初始化。这里是你的代码:如果controlIndex
是5
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
来解决此问题。那样的话,你想assert
那controlIndex == 4
。或者,您可以在if
之前将previewViewImage
和activityIndicator
初始化为一些合理的默认值。
该死的感谢了很多。改变最后的其他人,如果如果作品像魅力。到目前为止,不知道Xcode/Swift是如此严格... –
Swift只是帮你一个忙。它通过坚持已经正确初始化变量来消除很难找到错误的可能性。 – vacawama
这是Swift风格,在行末尾处留下分号。 – vacawama