问题描述
我已阅读有关Swift的但没有看到任何明确的正常,不可用的品种。我有没有想过的东西,或者是常规的初始化器不支持具有原始值的枚举器,即使没有可能的初始化程序可能会失败(由于初始化程序违反默认值):
{pre>
enum FailableSeason:Int {
init?(d:Int){
switch(d%365){
case 60 ... 151:
self = .Spring
case 152 ... 243:
self = .Summer
case 244 ... 334:
self = .Fall
//默认提供,保证有效
默认值:
self = .Winter
}
}
case Spring
case Summer
case秋天
case冬天
}
枚举ImplicitlyUnwrappedFailableSeason:Int {
init!(d:Int){
switch(d%365){
case 60 ... 151:
self = .Spring
case 152。 .243:
self = .Summ呃
case 244 ... 334:
self = .Fall
//默认提供,保证有效
默认值:
self =。冬天
}
}
案例春季
案例夏季
案件秋季
案件冬季
}
enum NonFailableSeason:Int {
init(d:Int){
switch(d%365){
case 60 ... 151:
self = .Spring
case 152 ... 243:
self = .Summer
case 244 ... 334:
self = .Fall
//默认提供,保证有效
默认值:
self = .Winter
}
}
case Spring
case夏天
case秋天
case冬天
}
让thisWorks = FailableSeason(d:60)
let thisFails = ImplicitlyUnwrappedFailableSeason(d: 60)
let andThisFails = NonFailableSeason(d:0)
请注意,我将它分成单独的枚举,以确保它不是冲突的初始化器的问题
以前用于Swift 1.1(Xcode 6.1.1)但在Swift 1.2(Xcode 6.3)中失败。
这是一个在Apple开发人员论坛上讨论的错误: Swift 1.2 - 初始化枚举
该讨论中给出了两种解决方法:使用模块/应用程序名称
作为前缀:
let workaround1 = NameOfYourModule.NonFailableSeason(d:0)
或者显式调用init方法:
let workaround2 = NonFailableSeason.init(d:0)
更新:这在Xcode 6.3 beta 2(6D532l)版本中已经修复。
I have read documentation on Swift's failable initializer's but didn't see anything explicit about the normal, non-failable variety. Did I miss something, or are regular initializer not supported for enumerators with raw values, even when there is no possibility that the initializer could fail (since the initializer defers to a default value):
enum FailableSeason : Int {
init?(d: Int) {
switch (d % 365) {
case 60...151:
self = .Spring
case 152...243:
self = .Summer
case 244...334:
self = .Fall
// default provided, guaranteed to be valid
default:
self = .Winter
}
}
case Spring
case Summer
case Fall
case Winter
}
enum ImplicitlyUnwrappedFailableSeason : Int {
init!(d: Int) {
switch (d % 365) {
case 60...151:
self = .Spring
case 152...243:
self = .Summer
case 244...334:
self = .Fall
// default provided, guaranteed to be valid
default:
self = .Winter
}
}
case Spring
case Summer
case Fall
case Winter
}
enum NonFailableSeason : Int {
init(d: Int) {
switch (d % 365) {
case 60...151:
self = .Spring
case 152...243:
self = .Summer
case 244...334:
self = .Fall
// default provided, guaranteed to be valid
default:
self = .Winter
}
}
case Spring
case Summer
case Fall
case Winter
}
let thisWorks = FailableSeason(d: 60)
let thisFails = ImplicitlyUnwrappedFailableSeason(d: 60)
let andThisFails = NonFailableSeason(d: 0)
Note that I broke this into separate enums to make sure that it wasn't a problem of conflicting initializers.
This used to work with Swift 1.1 (Xcode 6.1.1) but fails in Swift 1.2 (Xcode 6.3).It is a bug which has been discussed in the Apple Developer Forum: Swift 1.2 - Initializing Enums.
Two workarounds are given in that discussion: Use the module/app nameas a prefix:
let workaround1 = NameOfYourModule.NonFailableSeason(d: 0)
Or call the init method explicitly:
let workaround2 = NonFailableSeason.init(d: 0)
Update: This has been fixed in the Xcode 6.3 beta 2 (6D532l) release.
这篇关于Swift枚举:普通/非可用的初始化程序不支持 - 或只是破碎?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!