Error[8]: Undefined offset: 6, File: /www/wwwroot/outofmemory.cn/tmp/plugin_ss_superseo_model_superseo.php, Line: 121
File: /www/wwwroot/outofmemory.cn/tmp/plugin_ss_superseo_model_superseo.php, Line: 473, decode(

如何枚举String类型的枚举? 迅捷4.2+

从Swift 4.2(使用Xpre
10)开始,只需添加协议一致性

CaseIterable
即可从中受益
allCases
。要添加此协议一致性,您只需要在某处写:

extension Suit: CaseIterable {}

如果枚举是您自己的,则可以直接在声明中指定一致性:

enum Suit: String, CaseIterable { case spades = "♠"; case hearts = "♥"; case diamonds = "♦"; case clubs = "♣" }

然后,以下代码将打印所有可能的值:

Suit.allCases.forEach {    print(
#if !swift(>=4.2)public protocol CaseIterable {    associatedtype AllCases: Collection where AllCases.Element == Self    static var allCases: AllCases { get }}extension CaseIterable where Self: Hashable {    static var allCases: [Self] {        return [Self](AnySequence { () -> AnyIterator<Self> in var raw = 0 var first: Self? return AnyIterator {     let current = withUnsafeBytes(of: &raw) { [+++].load(as: Self.self) }     if raw == 0 {         first = current     } else if current == first {         return nil     }     raw += 1     return current }        })    }}#endif
.rawValue)}

与早期Swift版本(3.x和4.x)的兼容性

如果您需要支持Swift 3.x或4.0,则可以通过添加以下代码来模仿Swift 4.2的实现:

[+++]


)
File: /www/wwwroot/outofmemory.cn/tmp/route_read.php, Line: 126, InsideLink()
File: /www/wwwroot/outofmemory.cn/tmp/index.inc.php, Line: 165, include(/www/wwwroot/outofmemory.cn/tmp/route_read.php)
File: /www/wwwroot/outofmemory.cn/index.php, Line: 30, include(/www/wwwroot/outofmemory.cn/tmp/index.inc.php)
Error[8]: Undefined offset: 7, File: /www/wwwroot/outofmemory.cn/tmp/plugin_ss_superseo_model_superseo.php, Line: 121
File: /www/wwwroot/outofmemory.cn/tmp/plugin_ss_superseo_model_superseo.php, Line: 473, decode(

如何枚举String类型的枚举? 迅捷4.2+

从Swift 4.2(使用Xpre
10)开始,只需添加协议一致性

CaseIterable
即可从中受益
allCases
。要添加此协议一致性,您只需要在某处写:

extension Suit: CaseIterable {}

如果枚举是您自己的,则可以直接在声明中指定一致性:

enum Suit: String, CaseIterable { case spades = "♠"; case hearts = "♥"; case diamonds = "♦"; case clubs = "♣" }

然后,以下代码将打印所有可能的值:

Suit.allCases.forEach {    print(
#if !swift(>=4.2)public protocol CaseIterable {    associatedtype AllCases: Collection where AllCases.Element == Self    static var allCases: AllCases { get }}extension CaseIterable where Self: Hashable {    static var allCases: [Self] {        return [Self](AnySequence { () -> AnyIterator<Self> in var raw = 0 var first: Self? return AnyIterator {     let current = withUnsafeBytes(of: &raw) { .load(as: Self.self) }     if raw == 0 {         first = current     } else if current == first {         return nil     }     raw += 1     return current }        })    }}#endif
.rawValue)}

与早期Swift版本(3.x和4.x)的兼容性

如果您需要支持Swift 3.x或4.0,则可以通过添加以下代码来模仿Swift 4.2的实现:

[+++]


)
File: /www/wwwroot/outofmemory.cn/tmp/route_read.php, Line: 126, InsideLink()
File: /www/wwwroot/outofmemory.cn/tmp/index.inc.php, Line: 165, include(/www/wwwroot/outofmemory.cn/tmp/route_read.php)
File: /www/wwwroot/outofmemory.cn/index.php, Line: 30, include(/www/wwwroot/outofmemory.cn/tmp/index.inc.php)
如何枚举String类型的枚举?_随笔_内存溢出

如何枚举String类型的枚举?

如何枚举String类型的枚举?,第1张

如何枚举String类型的枚举? 迅捷4.2+

从Swift 4.2(使用Xpre
10)开始,只需添加协议一致性

CaseIterable
即可从中受益
allCases
。要添加此协议一致性,您只需要在某处写:

extension Suit: CaseIterable {}

如果枚举是您自己的,则可以直接在声明中指定一致性:

enum Suit: String, CaseIterable { case spades = "♠"; case hearts = "♥"; case diamonds = "♦"; case clubs = "♣" }

然后,以下代码将打印所有可能的值:

Suit.allCases.forEach {    print(
#if !swift(>=4.2)public protocol CaseIterable {    associatedtype AllCases: Collection where AllCases.Element == Self    static var allCases: AllCases { get }}extension CaseIterable where Self: Hashable {    static var allCases: [Self] {        return [Self](AnySequence { () -> AnyIterator<Self> in var raw = 0 var first: Self? return AnyIterator {     let current = withUnsafeBytes(of: &raw) { .load(as: Self.self) }     if raw == 0 {         first = current     } else if current == first {         return nil     }     raw += 1     return current }        })    }}#endif
.rawValue)}

与早期Swift版本(3.x和4.x)的兼容性

如果您需要支持Swift 3.x或4.0,则可以通过添加以下代码来模仿Swift 4.2的实现:



欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/zaji/5646484.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-12-16
下一篇 2022-12-16

发表评论

登录后才能评论

评论列表(0条)

保存