Based off this question regarding an animated Progressbar WebView in Swift, I tried to implement the code from the chosen answer. The build failed and I received the following error in Xcode 6.1.1:
Class 'WebView' has no initializers
Xcode prompts me to then input this code:
required init(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
The build succeeds but then the app crashes when I try to load the aforementioned WebView. It gives me this error:
Thread 1: EXC_BAD_INSTRUCTION
Starscream later clarified that if the variables are added to a class "they need to be initialized at init time." Wasn't sure how to initialize the variables, so here was my attempt:
class WebView: UIViewController {
required init(coder aDecoder: NSCoder) {
var theBool: Bool = false
var myTimer = NSTimer()
}
@IBOutlet var webView: UIWebView!
@IBOutlet var myProgressView: UIProgressView!
var theBool: Bool
var myTimer: NSTimer
override func viewDidLoad() {
super.viewDidLoad()
let url = NSURL(string: "http://www.stackoverflow.com")
let request = NSURLRequest(URL: url!)
webView.loadRequest(request)
}
func funcToCallWhenStartLoadingYourWebview() {
self.myProgressView.progress = 0.0
self.theBool = false
self.myTimer = NSTimer.scheduledTimerWithTimeInterval(0.01667, target: self, selector: "timerCallback", userInfo: nil, repeats: true)
}
func funcToCallCalledWhenUIWebViewFinishesLoading() {
self.theBool = true
}
func timerCallback() {
if self.theBool {
if self.myProgressView.progress >= 1 {
self.myProgressView.hidden = true
self.myTimer.invalidate()
} else {
self.myProgressView.progress += 0.1
}
} else {
self.myProgressView.progress += 0.05
if self.myProgressView.progress >= 0.95 {
self.myProgressView.progress = 0.95
}
Which brings this error:
Super.init isn't called before returning from initializer.
How do I go about correctly initializing the variables? Sorry if this was overly longwinded.