这是我的代码
import UIKit
class ViewController: UIViewController {
@IBOutlet weak var timerLabel: UILabel!
@IBOutlet weak var timerButton: UIButton!
var timer : NSTimer?
var ms = 0
override func viewDidLoad() {
timerLabel.text = "00:00"
self.view.backgroundColor = UIColor(patternImage: UIImage(named: "background.png")!)
}
@IBAction func buttonTapped(sender: AnyObject) {
if timerButton.currentTitle == "Stopp" {
timer?.invalidate()
timerButton.setTitle("Start", forState:UIControlState.Normal)
} else {
timerButton.setTitle("Stopp", forState:UIControlState.Normal)
self.timer = NSTimer.scheduledTimerWithTimeInterval(0.01, target: self, selector: Selector("update"), userInfo: nil, repeats: true)
}
}
@IBAction func restartButtonTapped(sender: AnyObject){
if timerLabel.text != "00:00" && ms != 0 {
timer?.invalidate()
timerLabel.text = "00:00"
ms = 0
timerButton.setTitle("Start", forState:UIControlState.Normal)
}
}
func update() {
self.ms++
timerLabel.text = String(format: "%02d:%02d", ms / 100, ms % 100)
}
}
我收到此错误:
致命错误:在展开Optional值时意外发现nil (lldb)
我不知道如何修复它。如果你能帮助我,我会很高兴的。
答案 0 :(得分:0)
看起来您正在尝试访问声明为“Implicitly Unwrapped Optional”的变量的值。当这样的变量变为零时,它会崩溃。请仔细检查您的图像是否存在。
使用图片!如果你保证图像?不是没有其他崩溃。我认为在你的情况下,图像是零,你试图强行打开它。
您必须进行零检查以避免此次崩溃。
ContextWrapper contextWrapper=new ContextWrapper(getApplicationContext());
File directory= contextWrapper.getDir("FileStorage", Context.MODE_PRIVATE);
File myInternalFile=new File(directory,"current.txt");
FileInputStream fis=new FileInputStream(myInternalFile);
BufferedReader reader=new BufferedReader(new InputStreamReader(fis));
String mLine = reader.readLine();
TextView tv = (TextView)findViewById(R.id.editText);
while(mLine != null)
{
if(mLine.isEmpty()) {
tv.append(" ");
String str = System.getProperty("line.separator");
tv.append(str);
}
else
tv.append(mLine);
mLine = reader.readLine();
}