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

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
4 messages Options
G G
Reply | Threaded
Open this post in threaded view
|

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

G G

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)


--
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 [hidden email].
To post to this group, send email to [hidden email].
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.
Reply | Threaded
Open this post in threaded view
|

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

Sam Stigler
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

On Sunday, June 14, 2015, G G <[hidden email]> wrote:

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)


--
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 <a href="javascript:_e(%7B%7D,&#39;cvml&#39;,&#39;swift-language%2Bunsubscribe@googlegroups.com&#39;);" target="_blank">swift-language+unsubscribe@....
To post to this group, send email to <a href="javascript:_e(%7B%7D,&#39;cvml&#39;,&#39;swift-language@googlegroups.com&#39;);" target="_blank">swift-language@....
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.

--
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 [hidden email].
To post to this group, send email to [hidden email].
To view this discussion on the web visit https://groups.google.com/d/msgid/swift-language/CALLvdPkUb103oROZBLvL%2BTnG-%3D0_v3Vz%2BM_JM7Vie7tNFKbQSg%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.
G G
Reply | Threaded
Open this post in threaded view
|

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

G G
In reply to this post by G G
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"

      }

   }


--
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 [hidden email].
To post to this group, send email to [hidden email].
To view this discussion on the web visit https://groups.google.com/d/msgid/swift-language/2777a353-afaa-45bd-a12d-65608a056312%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
G G
Reply | Threaded
Open this post in threaded view
|

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

G G
In reply to this post by Sam Stigler


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.

--
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 [hidden email].
To post to this group, send email to [hidden email].
To view this discussion on the web visit https://groups.google.com/d/msgid/swift-language/84dc5022-ed6f-4b37-bcfa-647c30102f38%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.