我在 Playground 上有一个 EXC_BAD_INSTRUCTION 和 Swift 2.1.1,需要帮助来识别它
I have a EXC_BAD_INSTRUCTION with Swift 2.1.1 on Playground and need help to identify it
这是我的游乐场代码:
import UIKit
class CellularAutomata{
var array = Array(count:1, repeatedValue:Array(count: 1, repeatedValue: Bool()))
var chanceToStartAlive = 0.45
var width = 384
var height = 384
init(){
self.array = Array(count:self.width, repeatedValue:Array(count: self.height, repeatedValue: Bool()))
initialise()
}
init(chanceToStartAlive chance: Double){
chanceToStartAlive = chance
self.array = Array(count:self.width, repeatedValue:Array(count: self.height, repeatedValue: Bool()))
initialise()
}
init(chanceToStartAlive chance: Double, width columns: Int, height rows: Int){
chanceToStartAlive = chance
self.array = Array(count:columns, repeatedValue:Array(count: rows , repeatedValue: Bool()))
initialise()
}
func initialise(){
for index in 0...array.count{
for jndex in 0...array[index].count{
if(randomCGDouble() < self.chanceToStartAlive){
array[index][jndex] = true
}
}
}
}
func randomCGDouble() -> Double {
return Double(arc4random()) / Double(UInt32.max)
}
}
在调用initialise() 函数时发生错误指令。我觉得我忽略了一些非常明显的事情,当有人向我解释时,我会面面相觑。
编辑:对不起大家!我发现我做了一些愚蠢的事情。我需要在数组上执行 .count - 1。就像我说的,脸掌。一旦我发现你可以在操场上激活控制台并最终可以获得一些错误的输出反馈,我就明白了这一点。
您必须将 initialise
更改为
func initialise(){
print(array.count)
for index in 0..<array.count{
for jndex in 0..<array[index].count{
if(randomCGDouble() < self.chanceToStartAlive){
array[index][jndex] = true
}
}
}
}
注意两个循环中的 0..<array
而不是之前的 0...array
。这将防止索引越界。
请注意,该数组非常大 - 请尝试使用 width
和 height
或 12
来测试它。
这是我的游乐场代码:
import UIKit
class CellularAutomata{
var array = Array(count:1, repeatedValue:Array(count: 1, repeatedValue: Bool()))
var chanceToStartAlive = 0.45
var width = 384
var height = 384
init(){
self.array = Array(count:self.width, repeatedValue:Array(count: self.height, repeatedValue: Bool()))
initialise()
}
init(chanceToStartAlive chance: Double){
chanceToStartAlive = chance
self.array = Array(count:self.width, repeatedValue:Array(count: self.height, repeatedValue: Bool()))
initialise()
}
init(chanceToStartAlive chance: Double, width columns: Int, height rows: Int){
chanceToStartAlive = chance
self.array = Array(count:columns, repeatedValue:Array(count: rows , repeatedValue: Bool()))
initialise()
}
func initialise(){
for index in 0...array.count{
for jndex in 0...array[index].count{
if(randomCGDouble() < self.chanceToStartAlive){
array[index][jndex] = true
}
}
}
}
func randomCGDouble() -> Double {
return Double(arc4random()) / Double(UInt32.max)
}
}
在调用initialise() 函数时发生错误指令。我觉得我忽略了一些非常明显的事情,当有人向我解释时,我会面面相觑。
编辑:对不起大家!我发现我做了一些愚蠢的事情。我需要在数组上执行 .count - 1。就像我说的,脸掌。一旦我发现你可以在操场上激活控制台并最终可以获得一些错误的输出反馈,我就明白了这一点。
您必须将 initialise
更改为
func initialise(){
print(array.count)
for index in 0..<array.count{
for jndex in 0..<array[index].count{
if(randomCGDouble() < self.chanceToStartAlive){
array[index][jndex] = true
}
}
}
}
注意两个循环中的 0..<array
而不是之前的 0...array
。这将防止索引越界。
请注意,该数组非常大 - 请尝试使用 width
和 height
或 12
来测试它。