主题1:EXC_BAD_INSTRUCTION Swift 2

时间:2015-11-17 21:13:19

标签: ios iphone swift

我试图创建一个应用程序,告诉特定日期之前的剩余时间。就像一些闹钟应用程序说它会花多少时间唤醒你。我在计算这个方法时遇到了问题所以我为它创建了自己的函数。 playground 上的 原始版 似乎工作正常:

//: Playground - noun: a place where people can play

import UIKit

var str = "Hello, playground"


let Test = NSCalendar.currentCalendar().component(NSCalendarUnit.Day, fromDate: NSDate())

func Calculation() {

    //Months
    var month: String {
        let dateFormatter = NSDateFormatter()
        dateFormatter.dateFormat = "MM"
        return dateFormatter.stringFromDate(NSDate())
    }

    print(month)

    //Days
    var day: String {
        let dateFormatter = NSDateFormatter()
        dateFormatter.dateFormat = "dd"
        return dateFormatter.stringFromDate(NSDate())
    }

    print(day)


    //Hours
    var hour: String {
        let dateFormatter = NSDateFormatter()
        dateFormatter.dateFormat = "hh"
        return dateFormatter.stringFromDate(NSDate())
    }

    print(hour)

    //Minutes
    var minute: String {
        let dateFormatter = NSDateFormatter()
        dateFormatter.dateFormat = "mm"
        return dateFormatter.stringFromDate(NSDate())
    }

    print(minute)

    //Current Time
    let CurrentMonths = Int(month)
    let CurrentDays = Int(day)
    let CurrentHours = Int(hour)
    let CurrentMinutes = Int(minute)

    //End Date
    let EndMonths = 11;
    let EndDays = 23;
    let EndHours = 12;
    let EndMinutes = 60;

    //Calculation

    var LeftMonths = EndMonths - CurrentMonths!
    var LeftDays = EndDays - CurrentDays! - 1
    var LeftHours = EndHours - CurrentHours! - 1
    var LeftMinutes = EndMinutes - CurrentMinutes! - 1

    //Update Labels



    //Re-Run Loop To Update Time
}
Calculation()

我不得不稍微更改它以便它可以作为Swift 2中的独立iOS应用程序。目前我有错误Thread 1: EXC_BAD_INSTRUCTION(code=EXC_1386_INVOP, subcode=0x0)在控制台中它说

fatal error: unexpectedly found nil while unwrapping an Optional value
(lldb) `. I checked the debug menu/console to the left of it and none of my variables are set to `nil.

这是我的ViewController.swift代码 更新代码修复本地Var问题

import UIKit

class ViewController: UIViewController {

    //Current Time
    var CurrentMonths:Int = 0
    var CurrentDays:Int = 0
    var CurrentHours:Int = 0
    var CurrentMinutes:Int = 0

    //End Date
    var EndMonths:Int = 0
    var EndDays:Int = 0
    var EndHours:Int = 0
    var EndMinutes:Int = 0

    //Calculation

    var LeftMonths:Int = 0
    var LeftDays:Int = 0
    var LeftHours:Int = 0
    var LeftMinutes:Int = 0

    //Define
    var month:String = ""
    var day:String = ""
    var hour:String = ""
    var minute:String = ""

    //Labels
    @IBOutlet weak var DayL: UILabel!
    @IBOutlet weak var HourL: UILabel!
    @IBOutlet weak var MinuteL: UILabel!


    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view, typically from a nib.
        Calculation()
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }

    //Months
    func Month() {
        var month: String {
            let dateFormatter = NSDateFormatter()
            dateFormatter.dateFormat = "MM"
            return dateFormatter.stringFromDate(NSDate())
        }
        print(month)
        Day()
    }

    //Days
    func Day() {
        var day: String {
            let dateFormatter = NSDateFormatter()
            dateFormatter.dateFormat = "dd"
            return dateFormatter.stringFromDate(NSDate())
        }
        print(day)
        Hour()
    }

    //Hours
    func Hour() {
        var hour: String {
            let dateFormatter = NSDateFormatter()
            dateFormatter.dateFormat = "hh"
            return dateFormatter.stringFromDate(NSDate())
        }
        print(hour)
        Minute()
    }
    //Minutes
    func Minute() {
        var minute: String {
            let dateFormatter = NSDateFormatter()
            dateFormatter.dateFormat = "mm"
            return dateFormatter.stringFromDate(NSDate())
        }
        print(minute)
    }

    func Calculation() {

        //Start Calculations
        Month()

        //Current Time
        CurrentMonths = Int(month)!
        CurrentDays = Int(day)!
        CurrentHours = Int(hour)!
        CurrentMinutes = Int(minute)!

        //End Date
        EndMonths = 11;
        EndDays = 23;
        EndHours = 12;
        EndMinutes = 60;

        //Calculation

        LeftMonths = EndMonths - CurrentMonths
        LeftDays = EndDays - CurrentDays - 1
        LeftHours = EndHours - CurrentHours - 1
        LeftMinutes = EndMinutes - CurrentMinutes - 1

        //Update Labels
        DayL.text = String(LeftDays)
        HourL.text = String(LeftHours)
        MinuteL.text = String(LeftMinutes)



        //Re-Run Loop To Update Time
        Calculation()
    }

}

CurrentMonths = Int(month)!发生错误 所有来自类似问题的解决方案似乎都不起作用。 此外,在计算功能中删除函数中的var标记时,它会给我一个错误,因此我无法删除它。

1 个答案:

答案 0 :(得分:0)

您的Calculation()功能存在范围问题。

如果您想在Month()中投放Day()Hour()Calculation()等,请从Calculation()范围内拨打电话。这些函数应该从类级别声明,而不是在Calculation()

中声明

此外,在每个函数中计算后,您都没有设置类变量MonthDay等。

这是您的完整代码:

import UIKit

class ViewController: UIViewController {

    //Current Time
    var CurrentMonths:Int = 0
    var CurrentDays:Int = 0
    var CurrentHours:Int = 0
    var CurrentMinutes:Int = 0

    //End Date
    var EndMonths:Int = 0
    var EndDays:Int = 0
    var EndHours:Int = 0
    var EndMinutes:Int = 0

    //Calculation

    var LeftMonths:Int = 0
    var LeftDays:Int = 0
    var LeftHours:Int = 0
    var LeftMinutes:Int = 0

    //Define
    var month:String = ""
    var day:String = ""
    var hour:String = ""
    var minute:String = ""

    //Labels
    @IBOutlet weak var DayL: UILabel!
    @IBOutlet weak var HourL: UILabel!
    @IBOutlet weak var MinuteL: UILabel!


    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view, typically from a nib.
        Calculation()
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }

    func Month() {
        var month: String {
            let dateFormatter = NSDateFormatter()
            dateFormatter.dateFormat = "MM"
            return dateFormatter.stringFromDate(NSDate())
        }
        self.month = month
        Day()
    }

    //Days
    func Day() {
        var day: String {
            let dateFormatter = NSDateFormatter()
            dateFormatter.dateFormat = "dd"
            return dateFormatter.stringFromDate(NSDate())
        }
        self.day = day
        Hour()
    }

    //Hours
    func Hour() {
        var hour: String {
            let dateFormatter = NSDateFormatter()
            dateFormatter.dateFormat = "hh"
            return dateFormatter.stringFromDate(NSDate())
        }
        self.hour = hour
        Minute()
    }
    //Minutes
    func Minute() {
        var minute: String {
            let dateFormatter = NSDateFormatter()
            dateFormatter.dateFormat = "mm"
            return dateFormatter.stringFromDate(NSDate())
        }
        self.minute = minute
    }


    func Calculation() {

        //Months
        Month()

        //Current Time
        CurrentMonths = Int(month)!
        CurrentDays = Int(day)!
        CurrentHours = Int(hour)!
        CurrentMinutes = Int(minute)!

        //End Date
        EndMonths = 11;
        EndDays = 23;
        EndHours = 12;
        EndMinutes = 60;

        //Calculation

        LeftMonths = EndMonths - CurrentMonths
        LeftDays = EndDays - CurrentDays - 1
        LeftHours = EndHours - CurrentHours - 1
        LeftMinutes = EndMinutes - CurrentMinutes - 1

        //Update Labels
        DayL.text = String(LeftDays)
        HourL.text = String(LeftHours)
        MinuteL.text = String(LeftMinutes)



        //Re-Run Loop To Update Time
        Calculation()
    }

}