I'm rendering some views that have a dynamic list of modifiers:
public enum Colors:String, CaseIterable {
public func to_color() -> Color {
color_hex(self.rawValue)
}
case black = "#000000"
case white = "#FFFFFF"
case gray_500 = "#F9FAFB"
case blue_200 = "#BFDBFE"
case blue_500 = "#3B82F6"
}
public enum Style {
case bg(Colors)
case text(Colors)
}
struct StyleModifier: ViewModifier {
fileprivate let properties: [Style]
func body(content: _ViewModifier_Content<StyleModifier>) -> some View {
return apply(content: content.toAnyView(), properties)
}
}
public extension View {
func toAnyView() -> AnyView {
return AnyView(self)
}
func style(_ clsName: [Style]) -> some View {
ModifiedContent(content: self, modifier: StyleModifier(properties: clsName))
}
func style2(_ clsName: [Style]) -> some View {
ModifiedContent(content: self, modifier: StyleModifier2(properties: clsName))
}
}
... and try to make this without resorting to AnyView but nothings get applied.
My working code with AnyView:
func apply(content: AnyView, _ properties: [Style]) -> AnyView {
return properties.reduce(content) { (v: AnyView, style: Style) -> AnyView in
switch style {
case .bg(let color):
return v.background(color.to_color()).toAnyView()
case .text(let color):
return v.foregroundColor(color.to_color()).toAnyView()
}
}
I try to translate this with generics:
func apply2<V: View>(of: V, _ properties: [Style]) -> V {
return properties.reduce(of) { (v: V, style: Style) -> V in
switch style {
case .bg(let color):
return v.background(color.to_color()) as! V
case .text(let color):
return v.foregroundColor(color.to_color()) as! V
}
}
But then this not work. Why?
I have a simple enum that I would like to iterate over. For this purpose, I've adopted Sequence and IteratorProtocol as shown in the code below. BTW, this can be copy/pasted to a Playground in Xcode 8.
import UIKit
enum Sections: Int {
case Section0 = 0
case Section1
case Section2
}
extension Sections : Sequence {
func makeIterator() -> SectionsGenerator {
return SectionsGenerator()
}
struct SectionsGenerator: IteratorProtocol {
var currentSection = 0
mutating func next() -> Sections? {
guard let item = Sections(rawValue:currentSection) else {
return nil
}
currentSection += 1
return item
}
}
}
for section in Sections {
print(section)
}
But the for-in loop generates the error message "Type 'Sections.Type' does not conform to protocol 'Sequence'".
The protocol conformance is in my extension; so, what is wrong with this code?
I know there are other ways of doing this but I'd like to understand what's wrong with this approach.
Thanks.
Note that Martin’s solution can be refactored as a protocol:
import Foundation
protocol EnumSequence
{
associatedtype T: RawRepresentable where T.RawValue == Int
static func all() -> AnySequence<T>
}
extension EnumSequence
{
static func all() -> AnySequence<T> {
return AnySequence { return EnumGenerator() }
}
}
private struct EnumGenerator<T: RawRepresentable>: IteratorProtocol where T.RawValue == Int {
var index = 0
mutating func next() -> T? {
guard let item = T(rawValue: index) else {
return nil
}
index += 1
return item
}
}
Then, given an enum
enum Fruits: Int {
case apple, orange, pear
}
you slap the protocol and a typealias:
enum Fruits: Int, EnumSequence {
typealias T = Fruits
case apple, orange, pear
}
Fruits.all().forEach({ print($0) }) // apple orange pear
Update: As of Swift 4.2, you can simply add protocol conformance
to CaseIterable, see How to enumerate an enum with String type?.
You can iterate over a value of a type which conforms to the Sequence
protocol. Therefore
for section in Sections.Section0 {
print(section)
}
would compile and give the expected result. But of course that is not
really what you want because the choice of the value is arbitrary and the
value itself not needed in the sequence.
As far as I know, there is no way to iterate over a type itself, so that
for section in Sections {
print(section)
}
compiles. That would require that the "metatype" Sections.Type conforms
to Sequence. Perhaps someone proves me wrong.
What you can do is to define a type method which returns a sequence:
extension Sections {
static func all() -> AnySequence<Sections> {
return AnySequence {
return SectionsGenerator()
}
}
struct SectionsGenerator: IteratorProtocol {
var currentSection = 0
mutating func next() -> Sections? {
guard let item = Sections(rawValue:currentSection) else {
return nil
}
currentSection += 1
return item
}
}
}
for section in Sections.all() {
print(section)
}
Simply add to enum:
static var allTypes: [Sections] = [.Section0, .Section1, .Section2]
And than you can:
Sections.allTypes.forEach { (section) in
print("\(section)")
}
This looks so much simpler:
public protocol EnumSequence {
init?(rawValue: Int)
}
public extension EnumSequence {
public static var items: [Self] {
var caseIndex: Int = 0
let interator: AnyIterator<Self> = AnyIterator {
let result = Self(rawValue: caseIndex)
caseIndex += 1
return result
}
return Array(interator)
}
}
Iterated upon the solutions above, see below a protocol that can be implemented by enumerations to add the allValues sequence but also to allow the possibility to convert to and from string value.
Very convenient for String-like enumerations which need to support objective c (only int enumerations are allowed there).
public protocol ObjcEnumeration: LosslessStringConvertible, RawRepresentable where RawValue == Int {
static var allValues: AnySequence<Self> { get }
}
public extension ObjcEnumeration {
public static var allValues: AnySequence<Self> {
return AnySequence {
return IntegerEnumIterator()
}
}
public init?(_ description: String) {
guard let enumValue = Self.allValues.first(where: { $0.description == description }) else {
return nil
}
self.init(rawValue: enumValue.rawValue)
}
public var description: String {
return String(describing: self)
}
}
fileprivate struct IntegerEnumIterator<T: RawRepresentable>: IteratorProtocol where T.RawValue == Int {
private var index = 0
mutating func next() -> T? {
defer {
index += 1
}
return T(rawValue: index)
}
}
For a concrete example:
#objc
enum Fruit: Int, ObjcEnumeration {
case apple, orange, pear
}
Now you can do:
for fruit in Fruit.allValues {
//Prints: "apple", "orange", "pear"
print("Fruit: \(fruit.description)")
if let otherFruit = Fruit(fruit.description), fruit == otherFruit {
print("Fruit could be constructed successfully from its description!")
}
}
If your enum is an Int based one, you can do an effective but slightly dirty trick like this.
enum MyEnum: Int {
case One
case Two
}
extension MyEnum {
func static allCases() -> [MyEnum] {
var allCases = [MyEnum]()
for i in 0..<10000 {
if let type = MyEnum(rawValue: i) {
allCases.append(type)
} else {
break
}
}
return allCases
}
}
Then loop over MyEnum.allCases()..
I would like to use enum with associated values for type-safe NSNotifications:
enum Notification {
case Foo(Int)
case Bar
var rawValue: String {
switch self {
case .Foo:
return "Foo"
case .Bar:
return "Bar"
}
}
var asNSNotification: NSNotification {
let userInfo = [String: AnyObject]()
switch self {
case let .Foo(intVal):
userInfo["intVal": intVal]
default:
break
}
return NSNotification(name: rawValue, object: nil, userInfo: userInfo)
}
init?(fromNSNotification n: NSNotification) {
switch n.name {
case .Bar:
self = .Bar
case .Foo(42): // some bogus value
let realValue = n.userInfo?["intVal"] ?? 0
self = .Foo(realValue)
default:
return nil
}
}
}
This should work, but it sure is an ugly piece of code. Anyone has ideas how to make it more elegant?
EDIT: the reason why I want to use enum is to make parameters of each notification type-safe.
By "more elegant" I mean:
Simplify rawValue property (avoid having to switch)
Avoid "bogus values" when referencing enum cases with associated values in
initializer.
Anything that would reduce verbosity and improve
readability.
Okay, here's how rawValue property can be simplified:
var rawValue: String {
return Mirror(reflecting: self).children.first.flatMap({ $0.label }) ?? "\(self)"
}
It seems to me that your enum is working too hard. This should be sufficient:
enum Notification {
case Foo(Int)
case Bar
func notification() -> NSNotification {
switch self {
case Foo(let intVal):
return NSNotification(name: "Foo", object: nil, userInfo: ["IntVal":intVal])
case Bar:
return NSNotification(name: "Bar", object: nil)
}
}
}
Adding the ability to supply a non-nil object is left as an exercise for the reader.
I think I have figured out how to make the handling of NSNotifications more elegant:
enum NotificationType: String {
case Foo
case Bar
}
enum Notification {
case Foo(Int)
case Bar
// the fragile part
var type: NotificationType {
let name = Mirror(reflecting: self).children.first.flatMap({ $0.label }) ?? "\(self)"
return NotificationType(rawValue: name)!
}
var asNSNotification: NSNotification {
let name = type.rawValue
let userInfo = [String: AnyObject]()
switch self {
case let .Foo(intVal):
userInfo["intVal": intVal]
default:
break
}
return NSNotification(name: name, object: nil, userInfo: userInfo)
}
init?(fromNSNotification n: NSNotification) {
let type = NotificationType(rawValue: n.name)!
switch type {
case .Bar:
self = .Bar
case .Foo:
let value = n.userInfo?["intVal"] ?? 0
self = .Foo(value)
default:
return nil
}
}
}
This solution relies on the convention that case names in Notification and NotificationType are the same. One might say that such design is fragile, but I think this is little tradeoff compared to what we achieve.
Below is a little helper class to handle subscription/unsubscription:
protocol NotificationReceiving: class {
func didReceiveNotification(notification: Notification)
}
class NotificationReceiver {
private weak var delegate: NotificationReceiving?
private(set) var subscriptions = Set<NotificationType>()
init(delegate: NotificationReceiving?) {
self.delegate = delegate
}
func subscribe(notificationTypes: NotificationType...) {
let nc = NSNotificationCenter.defaultCenter()
let doSubscribe: NotificationType -> Void = {
nc.addObserver(self, selector: "handleNotification:", name: $0.name, object: nil)
self.subscriptions.insert($0)
}
notificationTypes.forEach(doSubscribe)
}
func unsubscribe(notificationTypes: NotificationType...) {
let nc = NSNotificationCenter.defaultCenter()
if notificationTypes.isEmpty {
nc.removeObserver(self)
} else {
let doUnsubscribe: NotificationType -> Void = {
nc.removeObserver(self, name: $0.name, object: nil)
self.subscriptions.remove($0)
}
notificationTypes.forEach(doUnsubscribe)
}
}
#objc private func handleNotification(notification: NSNotification) {
if let n = Notification(fromNSNotification: notification) {
delegate?.didReceiveNotification(n)
}
}
}
This is how a client might look:
class Client: NotificationReceiving {
private var receiver: NotificationReceiver!
init() {
receiver = NotificationReceiver(delegate: self)
receiver.subscribe(.Foo, .Bar)
}
deinit {
receiver.unsubscribe()
}
func didReceiveNotification(notification: Notification) {
switch notification {
case let .Foo(val):
print("foo with val: \(val)")
// this notification is one-shot:
receiver.unsubscribe(.Foo)
case .Bar:
print("bar!")
}
}
}
Not sure handling all notifications in one method is good design (I guess it might get messy when there are many notifications), but the point is that we can now use the power of type safety and pattern matching, which is awesome.
Edit
A simple solutions was found thanks to #Airspeed Velocity, with a little twist added since this is parsed from JSON. Allow the initializer to take AnyObject? and default to Unknown (or Invalid):
init(value: AnyObject?) {
if let value = value as? Int
,let result = Result(rawValue: value) {
self = result
} else {
self = .Unknown
}
}
//
Original
I've implemented an enum with a Unknown case:
enum Result: Int {
case Success
case Failure
case Foo
case Bar
case FooBar
case FooFoo
...
case Unknown = -1
}
I want to create another initializer that takes an Int and returns Unknown for case not recognized by the enum:
init(value: Int) {
self.init(rawValue: value)
if self == nil { // Complication Error
self = .Unknown
}
}
My solution for now is a Factory method, however an initializer would be much cleaner:
static func resultWithValue(value: Int) -> Result {
if let result = self(rawValue: value) {
return result
}
return .Unknown
}
While you can’t delegate to a failable initializer, since enums are value types, you can just try creating another value of the same enum using the failable initializer, and then substitute a default in case of nil, and assign that to self:
enum Result: Int {
case Success
case Failure
case Unknown = -1
init(value: Int) {
self = Result(rawValue: value) ?? .Unknown
}
}
Result(value: 100) == .Unknown // returns true
Swift documentation says that classes, structs, and enums can all conform to protocols, and I can get to a point where they all conform. But I can't get the enum to behave quite like the class and struct examples:
protocol ExampleProtocol {
var simpleDescription: String { get set }
mutating func adjust()
}
class SimpleClass: ExampleProtocol {
var simpleDescription: String = "A very simple class."
var anotherProperty: Int = 69105
func adjust() {
simpleDescription += " Now 100% adjusted."
}
}
var a = SimpleClass()
a.adjust()
let aDescription = a.simpleDescription
struct SimpleStructure: ExampleProtocol {
var simpleDescription: String = "A simple structure"
mutating func adjust() {
simpleDescription += " (adjusted)"
}
}
var b = SimpleStructure()
b.adjust()
let bDescription = b.simpleDescription
enum SimpleEnum: ExampleProtocol {
case Base
var simpleDescription: String {
get {
return "A Simple Enum"
}
set {
newValue
}
}
mutating func adjust() {
self.simpleDescription += ", adjusted"
}
}
var c = SimpleEnum.Base
c.adjust()
let cDescription = c.simpleDescription
I haven't figured out how to get the simpleDescription to change as a result of calling adjust(). My example obviously won't do that because the getter has a value hard-coded, but how can I set a value for the simpleDescription while still conforming to the ExampleProtocol?
This is my attempt:
protocol ExampleProtocol {
var simpleDescription: String { get }
mutating func adjust()
}
enum ExampleEnum : ExampleProtocol {
case Base, Adjusted
var simpleDescription: String {
return self.getDescription()
}
func getDescription() -> String {
switch self {
case .Base:
return "A simple description of enum"
case .Adjusted:
return "Adjusted description of enum"
}
}
mutating func adjust() {
self = ExampleEnum.Adjusted
}
}
var c = ExampleEnum.Base
c.adjust()
let cDescription = c.simpleDescription
Here is my take at it.
As this is an enum and not a class, you have to think different(TM): it is your description that has to change when the "state" of your enum changes (as pointed out by #hu-qiang).
enum SimpleEnumeration: ExampleProtocol {
case Basic, Adjusted
var description: String {
switch self {
case .Basic:
return "A simple Enumeration"
case .Adjusted:
return "A simple Enumeration [adjusted]"
}
}
mutating func adjust() {
self = .Adjusted
}
}
var c = SimpleEnumeration.Basic
c.description
c.adjust()
c.description
Hope that helps.
Here's another approach, using only the knowledge gained from the tour until that point*
enum SimpleEnumeration: String, ExampleProtocol {
case Basic = "A simple enumeration", Adjusted = "A simple enumeration (adjusted)"
var simpleDescription: String {
get {
return self.toRaw()
}
}
mutating func adjust() {
self = .Adjusted
}
}
var c = SimpleEnumeration.Basic
c.adjust()
let cDescription = c.simpleDescription
If you want to have adjust() act as a toggle (although there's nothing to suggest this is the case), use:
mutating func adjust() {
switch self {
case .Basic:
self = .Adjusted
default:
self = .Basic
}
}
*(Although it doesn't explicitly mention how to specify a return type and a protocol)
Here's a solution that doesn't change the current enum value, but their instance values instead (just in case it is useful to anyone).
enum ProtoEnumeration : ExampleProtocol {
case One(String)
case Two(String)
var simpleDescription: String {
get {
switch self {
case let .One(desc):
return desc
case let .Two(desc):
return desc
}
}
}
mutating func adjust() {
switch self {
case let .One(desc):
self = .One(desc + ", adjusted 1")
case let .Two(desc):
self = .Two(desc + ", adjusted 2")
}
}
}
var p = ProtoEnumeration.One("test")
p.simpleDescription
p.adjust()
p.simpleDescription
It is not possible to define variables without getter and setter in enums and therefore it is impossible to have a variable that you can modify.
You can conform to the protocol but you cannot have same behavior with mutating as in classes.
It is a link about enum in swift.
Structures and enumerations are value types. By default, the properties of a value type cannot be modified from within its instance methods. link
Then, you have to use mutating function.
enum ProtocolEnum: ExampleProtocol {
case on, off
var simpleDescription: String {
switch self {
case .on:
return "Switch is ON"
case .off:
return "Switch is OFF"
}
}
mutating func adjust() {
switch self {
case .on:
self = off
case .off:
self = on
}
}
}
var c = ProtocolEnum.on
c.simpleDescription
c.adjust()
let cDescription = c.simpleDescription
Another option is for adjust() to flip between cases as follows:
enum SimpleEnum: ExampleProtocol {
case Foo, Bar
var simpleDescription: String {
get {
let value = self == .Foo
? "Foo"
: "Bar"
return "A simple \(value) enum."
}
}
mutating func adjust() {
self = self == .Foo
? .Bar
: .Foo
}
}
Here's building on Jack's answer:
protocol ICanWalk {
var description: String { get }
mutating func stepIt()
}
enum TwoStepsForwardThreeStepsBack: Int, ICanWalk {
case Base = 0, Step1, Step2
var description: String {
return "Step \(self.rawValue)"
}
mutating func stepIt() {
if let nextStep = TwoStepsForwardThreeStepsBack( rawValue: self.rawValue + 1 ) {
// going forward.
self = nextStep
} else {
// back to the base.
self = TwoStepsForwardThreeStepsBack.Base
}
}
}
I came up with this
protocol ExampleProtocol {
var simpleDescription: String { get }
mutating func adjust()
}
enum Seat: ExampleProtocol {
case WindowSeat, MiddleSeat, AisleSeat
var simpleDescription : String {
switch self {
case .WindowSeat:
return "Window Seat"
case .MiddleSeat:
return "Middle Seat"
case .AisleSeat:
return "Aisle Seat"
}
}
mutating func adjust() {
switch self {
case .WindowSeat:
self = .MiddleSeat
case .MiddleSeat:
self = . AisleSeat
case .AisleSeat:
self = .WindowSeat
}
}
}
var seat = Seat.MiddleSeat
print(seat.simpleDescription) // Middle Seat
seat.adjust()
print(seat.simpleDescription) // Aisle Seat
Another variation: Using associated values to hold and display previous option
(of the form "Selected 1, adjusted from 2, adjusted from 1, adjusted from 2, adjusted from 1")
protocol ExampleProtocol {
var simpleDescription: String { get }
mutating func adjust()
}
indirect enum EnumWithDescription: ExampleProtocol {
case option1(EnumWithDescription?)
case option2(EnumWithDescription?)
var simpleDescription: String {
return "Selected " + getDescription()
}
internal func getDescription() -> String {
var currentValue: String
let previousValue : EnumWithDescription?
switch self {
case .option1(let previous):
currentValue = "1"
previousValue = previous
case .option2(let previous):
currentValue = "2"
previousValue = previous
}
if let adjustedFrom = previousValue?.getDescription() {
return "\(currentValue) adjusted from \(adjustedFrom)"
}
else {
return "\(currentValue)"
}
}
mutating func adjust() {
switch self {
case .option1:
self = .option2(self)
case .option2:
self = .option1(self)
}
}
}
var d = EnumWithDescription.option1(nil)
d.simpleDescription
d.adjust()
d.adjust()
d.simpleDescription
// Output: "Selected 1, adjusted from 2, adjusted from 1, adjusted from 2, adjusted from 1"
here's my code
enum SimpleEnum: ExampleProtocol {
case Base, Adjusted
var simpleDescription: String {
get {
var description = "A simple enum."
switch self {
case .Base:
return description
case .Adjusted:
return description + " - [adjusted]"
}
}
}
mutating func adjust() {
self = SimpleEnum.Adjusted
}
}
var simpleEnum = SimpleEnum.Base
simpleEnum.adjust()
simpleEnum.simpleDescription
My first contribution here:
enum SimpleEnum: ExampleProtocol {
case Basic(String), Adjusted(String)
init() {
self = SimpleEnum.Basic("A simple Enum")
}
var simpleDescription: String {
get {
switch self {
case let .Basic(string):
return string
case let .Adjusted(string):
return string
}
}
}
mutating func adjust() {
self = SimpleEnum.Adjusted("full adjusted")
}
}
var c = SimpleEnum()
c.adjust()
let cDescription = c.simpleDescription
Thanks for others!
This experiment threw me off too, due to the previous SimpleClass and SimpleStructure examples showing the property simpleDescription being modified internally, which caused me to think that I needed to do the same thing. After looking over the other answers posted here and reading the official Apple Swift 2.1 documentation, I came up with this:
protocol ExampleProtocol {
var simpleDescription: String { get }
mutating func adjust()
}
enum SimpleEnum: ExampleProtocol {
case Simple
case Adjusted
var simpleDescription: String {
switch self {
case .Simple:
return "A simple enumeration"
case .Adjusted:
return "A simple enumeration somewhat changed."
}
}
mutating func adjust() {
self = .Adjusted
}
mutating func restore() {
self = .Simple
}
}
var d: SimpleEnum = .Simple
d.simpleDescription
d.adjust()
d.simpleDescription
d.restore()
d.simpleDescription
Also notice that in the examples given by Apple for SimpleClass and SimpleStructure prior to this experiment, the simple description is lost internally - you cannot get the original value back (unless of course you save it outside of the class/structure); this is what prompted me to create a restore() method for the SimpleEnum example, which allows you to toggle it back and forth between values. Hope this is useful to someone!
I was thinking that the goal is simply to retain state and use a description to make the current state easier to read:
enum SimpleEnum: ExampleProtocol {
case Default, Adjusted
init() {
self = .Default
}
var simpleDescription: String { get { return "\(self) Value" }}
mutating func adjust() {
self = .Adjusted
}
}
var simpleEnum = SimpleEnum()
simpleEnum.adjust()
let adjustedSimple = simpleEnum.simpleDescript
how about this
enum SimpleEnum : ExampleProtocol {
case Desc(String)
init() {
self = Desc("a simple enum")
}
var simpleDescription:String {
get {
return (Mirror(reflecting: self).children.first!.value as? String)!
}
}
mutating func adjust() {
self = SimpleEnum.Desc(self.desc + " adjusted")
}
}
var e = SimpleEnum()
e.simpleDescription # => "a simple enum"
e.adjust()
e.simpleDescription # => "a simple enum adjusted"