0

Same piece of code can't run on playground but can in the xcode project.

There is error"Excuetion was interrupted.reason:EXC_BAD_INSTRUCTION...."

Here is the detail snapshot

Question here is to figure out why this issue happen ? Why exist in playground not in the xcode ?

Thanks enter image description here

Forrest
  • 122,703
  • 20
  • 73
  • 107

2 Answers2

0

You did not init inputArray[i]. Assigning to a non-existent array element crashes. Instead (I guess), append the value via

inputArray.append(Int(rand())

For sure it will also crash in code, not only in Playground.

As a side note. Write

var inputArray = [Int]() // init as used by Apple now with trailing braces
for i in 0..<10 { // use range instead
  inputArray.append(Int(rand())
}

to have it more Swiftly.

qwerty_so
  • 35,448
  • 8
  • 62
  • 86
0

When I write the same code on the playground (Xcode Version 6.1.1 (6A2008a)), error appear: "Use of unresolved identifier 'rand'".

Playground is just a "playgound", don't take it too serious. I am also confused at the some really weird problems that code works on normal Xcode project but does not work well on playground. Apple still need to improve the stability of playground although it is pretty cool.