EXC_BAD_INSTRUCTION (code=EXC_I386_INVOP,subcode =

2019-05-06 03:01发布

问题:

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]