from apple's book : is this right for color()

15 views
Skip to first unread message

G G

unread,
Jun 14, 2015, 7:16:39 AM6/14/15
to swift-l...@googlegroups.com

enum Suit {

   case Spades, Hearts, Diamonds, Clubs

   func simpleDescription() -> String {

      switch self {

      case .Spades:

         return "spade"

      case .Hearts:

         return "hearts"

      case .Diamonds:

         return "diamonds"

      case .Clubs:

         return "clubs"

      }

   }

   

   func color() -> String {

      switch self {

      case .Clubs:

         return "black"

      case .Spades:

         return "black"

      case .Hearts:

         return "red"

      case .Diamonds:

         return "red"

      }

   }

}


let hearts = Suit.Hearts


let heartsDescription = hearts.simpleDescription()

let spade = Suit.Spades

let spadeColor = spade.color()


println(heartsDescription)


println(spadeColor)


let diamond = Suit.Diamonds

let diamondColor = diamond.color()


println(diamondColor)


Sam Stigler

unread,
Jun 14, 2015, 11:06:35 AM6/14/15
to G G, swift-l...@googlegroups.com
It looks right to me, other than that personally I'd use computed properties instead of methods for both color() and simpleDescription(). Which part is making you question it?

Sam
--
You received this message because you are subscribed to the Google Groups "Swift Language" group.
To unsubscribe from this group and stop receiving emails from it, send an email to swift-languag...@googlegroups.com.
To post to this group, send email to swift-l...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/swift-language/cf3a2aa2-2a04-4cd3-8c3b-a696f416e508%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

G G

unread,
Jun 14, 2015, 1:50:44 PM6/14/15
to swift-l...@googlegroups.com
it just seems so odd, i guess. could you give me an idea when or why 
use functions with enum.I mean is that the only way to use enum types.

and is this not allowed:

   func color() -> String {

      switch self {

      case .Clubs:

      case .Spades:

         return "black"

      case .Hearts:

      case .Diamonds:

         return "red"

      }

   }


G G

unread,
Jun 14, 2015, 2:01:09 PM6/14/15
to swift-l...@googlegroups.com, gdo...@gmail.com, s...@stigler.org


On Sunday, June 14, 2015 at 11:06:35 AM UTC-4, Sam Stigler wrote:
It looks right to me, other than that personally I'd use computed properties instead of methods for both color() and simpleDescription(). Which part is making you question it?

Sam


Plus, shouldn't there be a case statement like

case Black, Red

somewhere like 

case Spades, Hearts, Diamonds, Clubs

thanks.
Reply all
Reply to author
Forward
0 new messages