gpt4 book ai didi

ios - 线程 1 : EXC_BAD_INSTRUCTION Swift 2

转载 作者:搜寻专家 更新时间:2023-11-01 05:38:38 24 4
gpt4 key购买 nike

我正在尝试制作一个应用程序,该应用程序会告诉您到特定日期为止的剩余时间。就像一些闹钟应用程序所说的那样,它会在多长时间内将您叫醒。我在计算方法上遇到了问题,所以我为它创建了自己的函数。我在 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()

我不得不稍微改变它,以便它可以作为独立的 iOS 应用程序在 Swift 2 中运行。目前我有错误 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 代码修复本地变量问题的更新代码:

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 标记时,它会给我一个错误,所以我无法删除它。

最佳答案

您的 Calculation() 函数存在范围问题。

如果要在Calculation()中运行Month()Day()Hour(),从 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()
}

}

关于ios - 线程 1 : EXC_BAD_INSTRUCTION Swift 2,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33767104/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com