我在我的代码中发现了一个非常奇怪的错误,它只发生在Release版本中.对我来说它看起来像一个Swift bug,但让我知道你的想法.
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])") } }
实例化AccessorBugTest时,应该打印
init: (3,3,3),(3,3)
但在Release版本中打印,
init: (3,0),3)
如果我删除未使用的属性状态和描述,那么问题是固定的,不知道为什么.此外,如果我使用x,y,z,w而不是枚举,那么它再次正常工作.
知道发生了什么事吗?
我已将程序上传到:https://github.com/endavid/AccessorBugTest
它包含一个测试用例,如果你在Release配置中运行它将失败(转到Program – > Scheme – > Edit Scheme,并将Test更改为Release而不是Debug).
我还下载了Xcode 7.1 beta,在Swift 2.0中试过,问题仍然存在:(