我意识到有几个"类似"的错误张贴在这个,但他们似乎没有帮助我在我的情况下。也有可能语法不像我期望的那样在Swift中工作:
没有编译错误或警告,只有一个运行时错误
var allPlayerSpawnState: Array<Array<Any>>!
var pName: String = "defaultPlayerName"
var pShape = "box"
var pDimensions = SCNVector4(1,1,1,0)
var pPosition = SCNVector3(0, 0, 0)
var pRotation = SCNVector3(0, 0, 0)
var pForce = SCNVector3(0, 0, 0)
var pTorque = SCNVector4(0, 0, 0, 0)
var pSkinArray = [UIColor]()
var pEmitter = false
var pEmitterUIColor = UIColor.random() // custom random color
var pState: SCNPhysicsBodyType = .Static
... values change here
print("n(pName)")
print(pShape)
print(pDimensions)
print(pPosition)
print(pRotation)
print(pForce)
print(pTorque)
print(pSkinArray)
print(pEmitter)
print(pEmitterUIColor)
print("(pState)n")
let tempArray: Array<Any> = [pName, pShape, pDimensions, pPosition, pRotation, pForce, pTorque, pSkinArray, pEmitter, pEmitterUIColor, pState]
allPlayerSpawnState.append(tempArray) // runtime error here: "Thread 1: EXE_BAD_INSTRUCTION (code=EXE_I386_INVOP, subcode=0x0)"
... later wanting to use it like this:
for i in allPlayerSpawnState {
some_function(i[0] as! String, pShape: i[1] as! String, pDimensions: i[2] as! SCNVector4, pPosition: i[3] as! SCNVector3, pRotation: i[4] as! SCNVector3, pForce: i[5] as! SCNVector3, pTorque: i[6] as! SCNVector4, pSkinArray: i[7] as! [UIColor], pEmitter: i[8] as! Bool, pEmitterUIColor: i[9] as! UIColor, pState: i[10] as! SCNPhysicsBodyType)
}
这里是输出
nameOfPlayer
sphere
SCNVector4(x: 1.0, y: 1.0, z: 1.0, w: 0.0)
SCNVector3(x: 0.0, y: -10.0, z: 0.0)
SCNVector3(x: 0.0, y: 0.0, z: 0.0)
SCNVector3(x: 0.0, y: 0.0, z: 0.0)
SCNVector4(x: 0.0, y: 0.0, z: 0.0, w: 0.0)
[UIDeviceRGBColorSpace 1 0 1 1]
true
UIDeviceRGBColorSpace 0 1 0 1
SCNPhysicsBodyType
fatal error: unexpectedly found nil while unwrapping an Optional value
当我选择单击数组中的元素时,它们都不是可选的,所以我不知道错误是想表明什么。
任何帮助都是感激的。如果你知道一种不使用"Any"的方法,可以帮助我防止在"……"之后指出的未来潜在问题。之后我想这样做:"
数组已声明但未初始化。现在你要把一些东西附加到数组中,它是nil
并导致运行时错误。
初始化数组:
var allPlayerSpawnState = Array<Array<Any>>()