线程崩溃在swift中乘以2个浮点数

时间:2015-04-13 19:53:22

标签: macos swift

因此,我正在构建打字程序,用户可以添加课程。现在他们正在加入他们的课程,我有一个"课程计算器"您输入首选的WPM(默认为40)和首选完成时间(默认为2分钟),然后计算您允许的字符数,并显示其他几个指标,以帮助您有效地构建课程。可能的。

我的信念是,这是swift 1.2中的一个错误,但我想在这里问一下,如果有一些明显的东西我不知道了。我有一个文本字段,您可以在其中键入首选的WPM,如果您在此文本字段中键入数字没有问题,如果您键入一个字母,但程序崩溃。

如果您考虑以下代码,这会非常奇怪。

infix operator =~ {}
var match :NSTextCheckingResult! = NSTextCheckingResult()
func =~ (input: String, pattern: String) -> Bool
{
    let regex = NSRegularExpression(pattern:pattern, options:nil, error:nil)!
    var matches = regex.matchesInString(input, options: nil, range: NSMakeRange(0, count(input))) as! Array<NSTextCheckingResult>
    match = regex.firstMatchInString(input, options: NSMatchingOptions.ReportProgress, range: NSMakeRange(0, count(input)))
    if (matches.count > 0)
    {
        return true
    }
    else
    {
        return false
    }
}

public func calculateAddLessonMetrics()
{
    //300 WPM is the average reading speed of a sighted person.
    //400 WPM average speed with VO.
    //125 WPM average braille reading speed.
    //(WPM * ((minutes * 60+seconds) / 60)) * 5 = number of chars you can use
    var preferredWPM:Float
    if (preferredWPMTextField.stringValue ~= "^(\\d+)$")
    {
        preferredWPM = (preferredWPMTextField.stringValue as NSString).floatValue
    }
    else
    {
        preferredWPM = 40.00
        println("Not a number")
    }
    preferredWPMTextField.stringValue = "\(preferredWPM)";
    var totalSeconds:Float
    if (preferredTimeTextField.stringValue ~= "^(\\d+):(\\d+)$")
    {
        var tempArray:NSArray = preferredTimeTextField.stringValue.componentsSeparatedByString(":");
        totalSeconds = (tempArray.objectAtIndex(0) as! NSString).floatValue * 60
        totalSeconds += (tempArray.objectAtIndex(1) as! NSString).floatValue
    }
    else
    {
        totalSeconds = 120
        var minutes  = totalSeconds / 60
        preferredTimeTextField.stringValue = "\(Int(minutes)):" + String(format: "%02d", Int((minutes*60) % 60))
    }
    var preferredTime:Float                        = totalSeconds / 60
    println("preferredWPM = \(preferredWPM) : preferredTime = \(preferredTime)");
    var maximumChars:Float                         = Float(preferredWPM) * preferredTime
    maximumChars                                   = maximumChars * 5
    var totalLength = 0;
    totalLength += count(addLessonObjectTextField.stringValue)
    if (temporaryLesson.count > 0)
    {
        for (var i:Int = 0; i < temporaryLesson.count;i++)
        {
            totalLength += count(temporaryLesson.objectAtIndex(i) as! String)
        }
    }
    var currentChars:Float                         = Float(totalLength) //what they're typing + what they've typed
    var currentWords:Float                         = currentChars / 5
    var requiredMinimumWPM:Float                   = currentWords / preferredTime
    var averageReadingTime:Float                   = currentWords / 300 //minute decimal
    var averageReadingTimeWithVoiceOver:Float      = currentWords / 400 //minute decimal
    var averageReadingTimeWithBrailleDisplay:Float = currentWords / 125 //minute decimal
    var lessonTime:Float                           = currentWords / Float(preferredWPM)
    var totalLessonTime:Float                      = lessonTime + averageReadingTime //minute decimal
    var totalLessonTimeWithVoiceOver:Float         = lessonTime + averageReadingTimeWithVoiceOver //minute decimal
    var totalLessonTimeWithBrailleDisplay:Float    = lessonTime + averageReadingTimeWithBrailleDisplay //minute decimal
    maximumCharactersTextField.stringValue         = "\(maximumChars)"
    currentCharactersTextField.stringValue         = "\(currentChars)"
    requiredWPMTextField.stringValue               = "\(requiredMinimumWPM)"
    readingTimeTextField.stringValue               = "\(Int(averageReadingTime)):" + String(format: "%02d", Int((averageReadingTime*60) % 60));
    readingTimeVOTextField.stringValue             = "\(Int(averageReadingTimeWithVoiceOver)):" + String(format: "%02d", Int((averageReadingTimeWithVoiceOver*60) % 60));
    readingTimeBrailleTextField.stringValue        = "\(Int(averageReadingTimeWithBrailleDisplay)):" + String(format: "%02d", Int((averageReadingTimeWithBrailleDisplay*60) % 60));
    totalTimeTextField.stringValue                 = "\(Int(totalLessonTime)):" + String(format: "%02d", Int((totalLessonTime*60) % 60));
    totalTimeVOTextField.stringValue               = "\(Int(totalLessonTimeWithVoiceOver)):" + String(format: "%02d", Int((totalLessonTimeWithVoiceOver*60) % 60));
    totalTimeBrailleTextField.stringValue          = "\(Int(totalLessonTimeWithBrailleDisplay)):" + String(format: "%02d", Int((totalLessonTimeWithBrailleDisplay*60) % 60));
}

如果我在preferredWPMTextField中输入一个字母,则日志会显示:

Not a number
preferredWPM = 40.0 : preferredTime = 2.0

然后线程在这一行崩溃:

        var maximumChars:Float = preferredWPM * preferredTime

带着疯狂有用的错误:

Thread 1: breakpoint 1.1

再次,如果我输入一个数字,它可以正常工作,

1 个答案:

答案 0 :(得分:0)

包含breakpoint的错误可能表示使用(无意)breakpoint

breakpoint

您可以再次左键单击禁用它们 如果要删除breakpoint,可以右键单击并选择删除 断点将停止(或暂停)代码,这对于调试情况非常有用。