Accessor gibt den falschen Wert in Swift 1.2 / 2.0-Release nur Build
Ich habe einen sehr seltsamen Fehler in meinem Code gefunden, der nur in Release-Builds auftritt. Es sieht für mich nach einem Swift-Bug aus, aber lass mich wissen, was du denkst.
import Foundation
enum Level : Int {
case
Bad = 0,
Normal = 1,
Good = 2,
Superb = 3
}
struct Attribute : Printable {
var x : Level = .Normal
var y : Level = .Normal
var z : Level = .Normal
var w : Level = .Normal
var description : String {
return "(\(x.rawValue), \(y.rawValue), \(z.rawValue), \(w.rawValue))"
}
func toString() -> String {
return description
}
}
var AccessorBugTestSingleton : AccessorBugTest!
class AccessorBugTest {
let index : Int
var attributes : [Attribute] = []
var todaysAttributes : Attribute {
get {
let r = attributes[index]
println("today: \(r)")
return r
}
}
var initialText : String = ""
// selection for key
var states : [String:Int] = ["x": 0, "y": 0, "z": 0, "w": 0]
var descriptions : [String:Int] = ["a": 0, "b": 0, "c": 0, "d": 0]
init() {
index = 10
for i in 1...31 {
var att = Attribute(x: .Superb, y: .Superb, z: .Superb, w: .Superb)
attributes.append(att)
}
let attribs = todaysAttributes
initialText = "\(attribs)"
println("init: \(attribs), \(self.attributes[index])")
}
}
Wenn AccessorBugTest instanziiert wird, sollte es drucken
init: (3, 3, 3, 3), (3, 3, 3, 3)
aber in Release wird es gedruckt,
init: (3, 0, 0, 0), (3, 3, 3, 3)
Wenn ich die nicht verwendeten Eigenschaften entfernestates
unddescriptions
, dann ist das Problem behoben, keine Ahnung warum. Auch wenn ich @ macx
, y
, z
, w
Ints statt Enums, dann funktioniert es wieder richtig.
Eine Vorstellung davon, was los ist?
Ich habe das Programm hochgeladen auf:https: //github.com/endavid/AccessorBugTes Es enthält einen Testfall, der fehlschlägt, wenn Sie ihn in der Release-Konfiguration ausführen (gehen Sie zu Programm -> Schema -> Schema bearbeiten und ändern Sie Test in Release anstelle von Debug).
Ich habe auch Xcode 7.1 Beta heruntergeladen, in Swift 2.0 ausprobiert und das Problem besteht immer noch: