EXC_BAD_INSTRUCTION (code=EXC_I386_INVOP,subcode =0x0) on Swift when working with multi-dimensional arrays

删除回忆录丶 提交于 2019-12-07 10:54:29

问题


I get this error when I try to create an array which contain arrays with enums.

To illustrate better here's the code:

let block1:Form[] = [Form.Circle, Form.Rectangle, Form.Triangle]
let block2:Form[] = [Form.Rectangle, Form.Circle, Form.Triangle]
let block3:Form[] = [Form.Rectangle, Form.Triangle, Form.Circle]
let block4:Form[] = [Form.Circle, Form.Triangle, Form.Rectangle]
let block5:Form[] = [Form.Triangle, Form.Circle, Form.Rectangle]
let block6:Form[] = [Form.Triangle, Form.Rectangle, Form.Circle]
var allBlocks:(Form[][])!

These are the arrays holding the enums and the last one will hold these arrays.

override func didMoveToView(view: SKView) {

          allBlocks = [block1, block2, block3, block4, block5, block6] //Error here
...
}

The error occurs when I try to assign the value to allBlocks

If I change the code to this I get no error:

let block1:Form[] = [Form.Circle, Form.Rectangle, Form.Triangle]
let block2:Form[] = [Form.Rectangle, Form.Circle, Form.Triangle]
let block3:Form[] = [Form.Rectangle, Form.Triangle, Form.Circle]
let block4:Form[] = [Form.Circle, Form.Triangle, Form.Rectangle]
let block5:Form[] = [Form.Triangle, Form.Circle, Form.Rectangle]
let block6:Form[] = [Form.Triangle, Form.Rectangle, Form.Circle]

override func didMoveToView(view: SKView) {

         var allBlocks = [block1, block2, block3, block4, block5, block6] //No error
...
}

But then I can't access the allBlocks variable in another place.

EDIT: In case it helps


回答1:


This sounds like a Swift compiler bug; the crash was due to an attempt to execute an illegal x86 instruction, so either the compiler generated invalid code or generated a branch to something that wasn't code at all or wasn't the beginning of an instruction.

Presumably you're beta-testing Xcode, so, if you don't already have an Apple Developer Connection account that lets you file bugs on Radar^WApple Bug Reporter, open an account, and then file a bug. (Apple may have given details on this as part of the Xcode download.)




回答2:


I think it is no longer an issue in XCode6 beta 6. Here is my test code:

enum Form: Int {
    case Circle=1
    case Rectangle
    case Triangle
}


func testEnumArray () {
    let block1:[Form] = [Form.Circle, Form.Rectangle, Form.Triangle]
    let block2:[Form] = [Form.Rectangle, Form.Circle, Form.Triangle]
    let block3:[Form] = [Form.Rectangle, Form.Triangle, Form.Circle]
    let block4:[Form] = [Form.Circle, Form.Triangle, Form.Rectangle]
    let block5:[Form] = [Form.Triangle, Form.Circle, Form.Rectangle]
    let block6:[Form] = [Form.Triangle, Form.Rectangle, Form.Circle]
    var allBlocks = [block1, block2, block3, block4, block5, block6]
    println(allBlocks)

}

It does not throw exception anymore.

In addition, there is a syntax change since the question was posted:

Instead of let block6:Form[] we need to write let block6:[Form]



来源:https://stackoverflow.com/questions/24223321/exc-bad-instruction-code-exc-i386-invop-subcode-0x0-on-swift-when-working-wit

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!