Create an account

Very important

  • To access the important data of the forums, you must be active in each forum and especially in the leaks and database leaks section, send data and after sending the data and activity, data and important content will be opened and visible for you.
  • You will only see chat messages from people who are at or below your level.
  • More than 500,000 database leaks and millions of account leaks are waiting for you, so access and view with more activity.
  • Many important data are inactive and inaccessible for you, so open them with activity. (This will be done automatically)


Thread Rating:
  • 346 Vote(s) - 3.49 Average
  • 1
  • 2
  • 3
  • 4
  • 5
How to make an enum conform to a protocol in Swift?

#11
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
Reply

#12
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
}
}
}

Reply

#13
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

Reply

#14
It is a [link][1] 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][2]

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


[1]:

[To see links please register here]

[2]:

[To see links please register here]

Reply

#15
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
}
}
Reply

#16
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.
Reply



Forum Jump:


Users browsing this thread:
3 Guest(s)

©0Day  2016 - 2023 | All Rights Reserved.  Made with    for the community. Connected through