John Dalton
John Dalton

Reputation: 150

Swift: Join method using parameter of type Array<Character> or Slice<Character> crashes in Release configuration

I have the following (simplified) code in Swift which works perfectly well in Debug configuration:

import Foundation

let charSlice = Array("Any string at all")
println( charSlice )
let str1 = "".join(charSlice.map{"\($0)"})
println (str1)

This produces the following output (as expected):

[A, n, y,  , s, t, r, i, n, g,  , a, t,  , a, l, l]
Any string at all
Program ended with exit code: 0

If I then change the Build Configuration to Release Mode I get a Run Time Error:

EXC_BAD_INSTRUCTION (code=EXC_I386_INVOP, subcode=0x0)

in the call to "".join

Am I doing something wrong or is this worth reporting as a bug?

Upvotes: 2

Views: 454

Answers (1)

Tanuj Mathur
Tanuj Mathur

Reputation: 1458

[Testing on XCode Beta 6]

This seems to be a bug in the optimized code generated by the compiler in the Release mode. If you change the Release mode optimization level to None (Build Settings > Swift Compiler - Code Generation > Optimization Level), the issue goes away.

Interestingly, .join() doesn't fail for null and empty strings, I assume because the method checks for them specifically as a performance optimization.

var baseString = ","
baseString.join([])    // works
baseString.join([""])  // works
baseString.join(["a"]) // fails

Feel free to report this as a bug.

Upvotes: 1

Related Questions