Xcode Playground:“错误:执行被中断,原因:EXC_BAD_INSTRUCTION (code=EXC_I386_INVOP, subcode=0x0)”

Posted

技术标签:

【中文标题】Xcode Playground:“错误:执行被中断,原因:EXC_BAD_INSTRUCTION (code=EXC_I386_INVOP, subcode=0x0)”【英文标题】:Xcode Playground: "error: Execution was interrupted, reason: EXC_BAD_INSTRUCTION (code=EXC_I386_INVOP, subcode=0x0)" 【发布时间】:2019-11-20 00:58:18 【问题描述】:

完整的错误信息:

error: Execution was interrupted, reason: EXC_BAD_INSTRUCTION (code=EXC_I386_INVOP, subcode=0x0).
The process has been left at the point where it was interrupted, use "thread return -x" to return to the state before expression evaluation.

在这行代码上 var x = isThereInterection(segments: lines)

这里有更多上下文:

class segment 

    var v1x:Double
    var v1y:Double
    var v2x:Double
    var v2y:Double
    var slope: Double
    var b: Double

    init(v1x: Double, v1y: Double, v2x: Double, v2y: Double) 
        self.v1x = v1x
        self.v1y = v1y
        self.v2x = v2x
        self.v2y = v2y
        self.slope = (v2y - v1y) / (v2x - v1x) //finding the slope for the line segment
        self.b = v1y - self.slope * v1x //finding the b value for the line function in y = mx + b
    



func isThereInterection(segments: [segment]) -> Bool
    var isThere:Bool?

    for seg1 in segments 
        for seg2 in segments 
            let leftSide = seg1.slope - seg2.slope
            let rightSide = seg2.b - seg1.b
            let xValue = rightSide/leftSide //this is the x value where the lines would intersect
            if((seg1.v1y...seg1.v2x).contains(xValue) && (seg2.v1x...seg2.v2x).contains(xValue)) //here we check if the x value of intersection is in range of both line segments
                return true //if it is in range of both line segments, we have an intersection so return true
            

        

    
    return false


var seg1 = segment(v1x: 3, v1y: 1, v2x: 1, v2y: 3)
var seg2 = segment(v1x: 1, v1y: 1, v2x: 3, v2y: 3)
var lines = [seg1, seg2]
var x = isThereInterection(segments: lines)
print(x)

但这很奇怪,因为当我将 seg1 更改为时代码运行良好 var seg1 = segment(v1x: 1, v1y: 3, v2x: 3, v2y: 1) 这些值应该是可以互换的,有人知道这里发生了什么吗?

我在网上 swift 操场上尝试过同样的事情,但同样的问题仍然存在,同时交换 seg1 的值有效。我要做的就是将一个段数组传递给我的辅助函数。

【问题讨论】:

刚刚发布, 是不是因为第一次比较中seg1和seg2是一样的? 【参考方案1】:

这是你的问题

for seg1 in segments 
    for seg2 in segments 
       // At the very first iteration of the loops, seg1 and seg2 are the same
        let leftSide = seg1.slope - seg2.slope
        let rightSide = seg2.b - seg1.b
        let xValue = rightSide/leftSide //this is the x value where the lines would intersect
        if((seg1.v1y...seg1.v2x).contains(xValue) && (seg2.v1x...seg2.v2x).contains(xValue)) //here we check if the x value of intersection is in range of both line segments
               return true //if it is in range of both line segments, we have an intersection so return true
         

    


如果您的输入段数组总是有两个元素,则无需循环。如果不是这种情况,您必须计算seg1 != seg2

【讨论】:

问题是lowerBound > upperBound,不知道如何解决 问题是你的 xValue 是哪个 NAN (Not A Number) 这不是问题,因为当数字被翻转时,xValue 等效于 -nan(如预期的那样),问题是当 v1x = 3 和 v2x = 1 时,它会尝试创建一个更低的范围bound = 3 和上限 = 1 我无法使用stride函数修复 我在段的构造函数中输入了一些登录信息,强制较小的 x 为 v1x,从而解决了问题

以上是关于Xcode Playground:“错误:执行被中断,原因:EXC_BAD_INSTRUCTION (code=EXC_I386_INVOP, subcode=0x0)”的主要内容,如果未能解决你的问题,请参考以下文章

我的 Xcode 6.0.1 Playground 有啥问题?

Xcode 6 Beta / Swift - Playground 未更新

如何设置 Xcode Playground 以稳定地使用构建的框架?

Xcode-playground 的时间线:白色背景上的白色文本

Xcode 7 Playground 中没有显示轴

Xcode Playground 中的 SwiftUI 列表