How can I implement a protocol, or alternative solutions, to have in each case all those of the parameter. Something like League.teamA (.allCases)
import Cocoa
enum PlayerA: CaseIterable {
case center
case powerForward
case smallForward
}
enum PlayerB: CaseIterable {
case pointGuard
case shootingGuard
}
protocol EnumProtocol {
var description: String { get }
}
enum League: EnumProtocol {
case teamA(PlayerA)
case teamB(PlayerB)
}
extension League {
var description: String {
switch self {
case let .teamA(player):
switch player {
case .center: return "Center"
case .smallForward: return "Small Forward"
case .powerForward: return "Power Forward"
}
case let .teamB(player):
switch player {
case .pointGuard: return "Point Guard"
case .shootingGuard: return "Shooting Guard"
}
}
}
}
League.teamA(.smallForward).description
//League.teamA(.allCases)
//League.teamB(.allCases)
If you want to be able to pass both a single instance an an array of instances, the easiest solution is to declare the associated type as an array, since then you can pass an array with a single element in case you just want to pass a single instance, or you can pass allCases as well.
enum PlayerA: CaseIterable {
case center
case powerForward
case smallForward
}
extension PlayerA: CustomStringConvertible {
var description: String {
switch self {
case .center: return "Center"
case .smallForward: return "Small Forward"
case .powerForward: return "Power Forward"
}
}
}
enum PlayerB: CaseIterable {
case pointGuard
case shootingGuard
}
extension PlayerB: CustomStringConvertible {
var description: String {
switch self {
case .pointGuard: return "Point Guard"
case .shootingGuard: return "Shooting Guard"
}
}
}
protocol EnumProtocol {
var description: String { get }
}
enum League: EnumProtocol {
case teamA([PlayerA])
case teamB([PlayerB])
}
extension League: CustomStringConvertible {
var description: String {
switch self {
case let .teamA(players):
return players.description
case let .teamB(players):
return players.description
}
}
}
League.teamA([.smallForward]).description
League.teamA(PlayerA.allCases)
League.teamB(PlayerB.allCases)
Related
I am trying to create an enum that returns a struct and I am not succeeding . I have checked a few questions similar to mine e.g here, and this here and they do not address the issue I have. This is my code:
import Foundation
struct Roll {
let times: String
init(with times: String) {
self.times = times
}
}
fileprivate enum Requests {
case poker
case cards
case slots
}
extension Requests: RawRepresentable {
typealias RawValue = Roll
init?(rawValue: RawValue) {
switch rawValue {
case Roll(with: "once"): self = .poker
case Roll(with: "twice"): self = .cards
case Roll(with: "a couple of times"): self = .slots
default: return nil
}
}
var rawValue: RawValue {
switch self {
case .poker: return Roll(with: "once")
case .cards: return Roll(with: "twice")
case .slots: return Roll(with: "a couple of times")
}
}
}
Then I would like to use it like : Requests.cards.rawValue
That's because of your structure Roll doesn't conform Equatable protocol and you're trying to make a comparison of it, just change it to
struct Roll: Equatable {
let times: String
init(with times: String) {
self.times = times
}
}
I'm trying to use nested enums to describe my model, in a way that makes illegal states impossible and provides categorisation at the top level. Right now, my (simplified) code is:
enum SportsEvent {
enum RunningEvent {
case sprint
case marathon
}
enum ThrowingEvent {
case hammer
case javelin
case discus
}
case running(event: RunningEvent)
case throwing(event: ThrowingEvent)
func isSameCategory(as other: SportsEvent) -> Bool {
return false
}
}
let sprint = SportsEvent.running(event: .sprint)
let javelin = SportsEvent.throwing(event: .javelin)
let hammer = SportsEvent.throwing(event: .hammer)
sprint.isSameCategory(as: javelin) // False
hammer.isSameCategory(as: javelin) // True
It feels like it should be trivial with an if case ... and a wildcard, but I'm not seeing how to achieve that. I'm hoping a giant switch statement isn't necessary, as my actual model is more complex.
I think you need a switch-statement, with a “compound case” listing all
possible “same value combinations” of the outer enumeration,
plus a default case:
func isSameCategory(as other: SportsEvent) -> Bool {
switch (self, other) {
case (.running, .running),
(.throwing, .throwing):
return true
default:
return false
}
}
Or (attribution goes to #Hamish):
func isSameCategory(as other: SportsEvent) -> Bool {
switch (self, other) {
case (.running, .running),
(.throwing, .throwing):
return true
case (.running, _),
(.throwing, _):
return false
}
}
which has the advantage that the compiler checks that all cases are covered. For an enumeration with n cases that makes 2 * n
cases in the switch statement (which is better than n * n if you checked all possible combinations).
Depending on your use case, you might be able to convert SportEvent to a protocol:
enum RunningEvent {
case sprint
case marathon
}
enum ThrowingEvent {
case hammer
case javelin
case discus
}
enum SportEventCategory {
case running
case throwing
}
protocol SportEvent {
var category: SportEventCategory { get }
}
extension RunningEvent: SportEvent {
var category: SportEventCategory {
return .running
}
}
extension ThrowingEvent: SportEvent {
var category: SportEventCategory {
return .throwing
}
}
let sportEvent1: SportEvent = RunningEvent.sprint
let sportEvent2: SportEvent = ThrowingEvent.hammer
print(sportEvent1.category == sportEvent2.category)
or even as one flat enum:
enum SportEvent {
enum Category {
case running
case throwing
}
case sprint
case marathon
case hammer
case javelin
case discus
var category: Category {
switch self {
case .sprint, .marathon, .hammer:
return .running
case .javelin, .discus:
return .throwing
}
}
}
let sportEvent1: SportEvent = .sprint
let sportEvent2: SportEvent = .marathon
print(sportEvent1.category == sportEvent2.category)
I'm learning swift through Apple's App Development book. There is a project for designing a Favorite Athlete App to view, add and edit athletes.
I'm wondering if there is a way to have a variable in a struct/class that is of a type that is dependent on the literal of another variable....
Ok, so this is what I'm thinking.
enum League {
case MLB, NFL, NBA
}
enum MLB {
case Braves, Yankees, Red Sox
}
enum NFL {
case Falcons, Giants, Patriots
}
enum NBA {
case Hawks, Knicks, Celtics
}
struct Athlete {
var name: String
var age: Int
var league: League
var Team: switch league{
case .MLB:
return MLB enum
case .NFL:
return NFL enum
case .NBA:
return NBA enum
}
}
You can't have spaces in enum cases. Per convention, you should use lowerCamelCase. Just from looking at your code, it looks like you're looking for something like this:
enum League {
case mlb, nfl, nba
}
protocol LeagueTeam {
static var league: League { get }
}
enum MLBTeams: LeagueTeam {
case braves, yankees, redSox
static let league = League.mlb
}
enum NFLTeams: LeagueTeam {
case falcons, giants, patriots
static let league = League.nfl
}
enum NBATeams: LeagueTeam {
case hawks, knicks, celtics
static let league = League.nba
}
struct Athlete {
var name: String
var age: Int
var team: LeagueTeam
var league: League { return type(of: team).league }
}
I like Alexander's solution, but this also lends itself nicely to enums with associated data:
enum MLBTeam {
case braves, yankees, redSox
}
enum NFLTeam {
case falcons, giants, patriots
}
enum NBATeam {
case hawks, knicks, celtics
}
enum Team {
case mlb(MLBTeam)
case nfl(NFLTeam)
case nba(NBATeam)
}
struct Athlete {
var name: String
var age: Int
var team: Team
}
let athlete = Athlete(name: "Julio Teherán", age: 26, team: .mlb(.braves))
With this, you can use the team property to switch both on specific teams and on leagues.
switch athlete.team {
case .mlb: print("Baseball")
case .nfl: print("Football")
case .nba: print("Basketball")
}
switch athlete.team {
case .mlb(.braves): print("Braves")
default: print("Not Braves")
}
The one sad thing in Swift 4 is that it still can't auto-generate Equatable for enums with associated values, so you have to do that by hand if you want it.
extension Team: Equatable {
static func ==(lhs: Team, rhs: Team) -> Bool {
switch (lhs, rhs) {
case let (.mlb(lhsTeam), .mlb(rhsTeam)): return lhsTeam == rhsTeam
case let (.nfl(lhsTeam), .nfl(rhsTeam)): return lhsTeam == rhsTeam
case let (.nba(lhsTeam), .nba(rhsTeam)): return lhsTeam == rhsTeam
default: return false
}
}
}
It's difficult to get Alexander's solution to allow == on LeagueTeam. Making LeagueTeam conform to Equatable creates all kinds of problems, and you can't create an exhaustive switch over LeagueTeam because it can have arbitrary implementations. So that's one nice thing about using an enum here.
On the other hand, using an enum pushes you into exhaustive switch statements, which may be a problem if the leagues or team list may change. (On the other hand, it may be a benefit…)
I want to determine if an enum is present in an enums list.
Intuitively I would do this:
if myEnum == (.one || .two) { return true }
else { return false }
Of course it doesn't work.
I know that I could do:
if myEnum == .one || myEnum == .two { return true }
else { return false }
Or
if [EnumType.one, EnumType.two].contains(myEnum) { return true }
else { return false }
But I just want to be fancy today :)
I am thinking about using filter but it seems overkill.
Do you have an idea?
Thanks a lot.
Thierry
You can do
//: Playground - noun: a place where people can play
import Cocoa
enum MyEnum {
case one
case two
case three
case four
}
let list: [MyEnum] = [.one, .two, .three, .four]
if list.contains(.one) {
// Contains
} else {
// Doesn't Contain
}
If you have associated data you have to make your enum be Equatable for this to work though. For example:
//: Playground - noun: a place where people can play
import Cocoa
enum MyEnum: Equatable {
case one
case two
case three
case four
case other(String)
static func ==(lhs: MyEnum, rhs: MyEnum) -> Bool {
switch (lhs, rhs) {
case (.one, .one),
(.two, .two),
(.three, .three),
(.four, .four):
return true
case (.other(let lhsString), .other(let rhsString)) where lhsString == rhsString:
return true
default:
return false
}
}
}
let list: [MyEnum] = [.one, .two, .three, .four, .other("test")]
if list.contains(.one) {
} else {
}
I would do a switch on each one and then have a default for if you can't find either of those types.
switch myEnum {
case .one:
print("One is here baby")
case .two:
print("Two is here baby")
default:
print("Can't find the case??????")
}
That's what OptionSet are for. It's technically a struct, but in usage, look very close to enum:
struct MyOptions : OptionSet {
var rawValue: Int
init(rawValue: Int) { self.rawValue = rawValue }
static let one = MyOptions(rawValue: 1)
static let two = MyOptions(rawValue: 2)
static let three = MyOptions(rawValue: 4)
}
let option: MyOptions = [.one, .two]
if option.contains([.one, .two]) {
print("Has one and two")
}
if !option.intersection([.one, .three]).isEmpty {
print("Has one or three")
}
I would use a switch as well and group the enum cases which are handled with common logic as follows:
enum MyEnum {
case one
case two
case three
case four
}
switch myEnum {
case .one, .two:
//deal with enum cases .one and .two
default:
//deal with all other cases
}
}
If you are trying to match arbitrary strings to various cases in your enum then you can do something like this (using Swift 3).
enum CompassPoint:String {
case north
case south
case east
case west
static func has(key: String) -> Bool {
for f in iterateEnum(CompassPoint.self) {
if(f.rawValue == key) {
return true;
}
}
return false;
}
private static func iterateEnum<T: Hashable>(_: T.Type) -> AnyIterator<T> {
var i = 0
return AnyIterator {
let next = withUnsafeBytes(of: &i) { $0.load(as: T.self) }
if next.hashValue != i { return nil }
i += 1
return next
}
}
}
CompassPoint.has(key: "south") // true
CompassPoint.has(key: "something") // false
Shoutout to #rintaro for his function for iterating over enums (found below).
https://stackoverflow.com/a/28341290/5270038
I have following code which basically defines enum for different companies and different methods for each company.
protocol Fetcher {
func getDetail()
static var idRegex:String {get}
}
class FooFetcher:Fetcher {
static var idRegex = "(^\\d{5}$)"
var company = Company.Foo
init (name:String?, id:String) {
println("Name: \(name) id: \(id)")
}
func getDetail() {
println("Foo Get detail")
}
}
class BarFetcher:Fetcher {
static var idRegex = "(^\\d{11}$)"
var company = Company.Bar
init (name:String?, id:String) {
println("Name: \(name) id: \(id)")
}
func getDetail() {
println("Bar Get detail")
}
}
enum Company:Int {
case Foo
case Bar
func fetcher(name:String?, id:String) -> AnyObject {
switch self {
case Foo:
return FooFetcher(name: name, id: id)
case .Bar:
return BarFetcher(name: name, id: id)
}
}
}
var compList = [Company.Foo , Company.Bar]
How can I get idRegex by using compList array only? I don't want to create another array like [FooFetcher.idRegex,BarFetcher.idRegex] because it will bloat the file. I want to use enum because it is easy to store their rawValue with Core Data.
I also wonder how to write better fetcher function for Company enum. Currently I use fetcher function like below
var company = Company.Foo
var strategy = company.fetcher("Some name", id: "Some id") as! Fetcher
strategy.getDetail()
AnyObject does not support Fetcher, in the company.fetcher function instead of returning AnyObject is -> Fetcher
enum Company:Int {
case Foo
case Bar
func fetcher(name:String?, id:String) -> Fetcher {
switch self {
case .Foo:
return FooFetcher(name: name, id: id)
case .Bar:
return BarFetcher(name: name, id: id)
}
}
}
FOO also there corresponds almost .foo
Along with eliasRuizHz's answer, add a new regex method to the Company enum.
func regex() -> String {
switch self {
case .Foo:
return FooFetcher.idRegex
case .Bar:
return BarFetcher.idRegex
}
}
The two responses both provide good contributions. For example, returning a Fetcher rather than AnyObject makes complete sense, as does a regex() function. The thing that I query, however, is whether you could use enums with associated values rather than this enum + class approach, but putting that to one side and answering the question in hand if you really want to directly access the .idRegex from an instance then it needs to be a regular instance variable not a class variable. One way to have it both ways is to return the type variable through an instance variable like so:
protocol Fetcher {
func getDetail()
var idRegex:String {get}
static var regex:String {get}
}
class FooFetcher:Fetcher {
static var regex = "(^\\d{5}$)"
var idRegex:String { return FooFetcher.regex }
var company = Company.Foo
init (name:String?, id:String) {
println("Name: \(name) id: \(id)")
}
func getDetail() {
println("Foo Get detail")
}
}
class BarFetcher:Fetcher {
static var regex = "(^\\d{11}$)"
var idRegex:String { return BarFetcher.regex }
var company = Company.Bar
init (name:String?, id:String) {
println("Name: \(name) id: \(id)")
}
func getDetail() {
println("Bar Get detail")
}
}
enum Company:Int {
case Foo
case Bar
func fetcher(name:String?, id:String) -> Fetcher {
switch self {
case Foo:
return FooFetcher(name: name, id: id)
case .Bar:
return BarFetcher(name: name, id: id)
}
}
}
var compList = [Company.Foo, Company.Bar]
var company = Company.Foo
var strategy = company.fetcher("Some name", id: "Some id")
strategy.getDetail()
strategy.idRegex
Edit Responding to Query
If you want the fetcher to not necessarily initialize an instance then set whatever requirement there is to determine whether a value or nil is returned.
enum Company:Int {
case Foo
case Bar
func fetcher(name:String?, id:String) -> Fetcher? {
switch self {
case Foo:
if FooFetcher.regex == "(^\\d{11}$)" {
return FooFetcher(name: name, id: id)}
else {return nil}
case .Bar:
if BarFetcher.regex == "(^\\d{11}$)" {
return BarFetcher(name: name, id: id)
}
else {return nil}
}
}
}
var compList = [Company.Foo, Company.Bar]
var company = Company.Foo
var strategy = company.fetcher("Some name", id: "Some id")
strategy?.getDetail()
strategy?.idRegex