• Open Menu Close Menu
  • Apple
  • Shopping Bag
  • Apple
  • Mac
  • iPad
  • iPhone
  • Watch
  • TV
  • Music
  • Support
  • Search apple.com
  • Shopping Bag

Lists

Open Menu Close Menu
  • Terms and Conditions
  • Lists hosted on this site
  • Email the Postmaster
  • Tips for posting to public mailing lists
Re: Question about enums
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Question about enums


  • Subject: Re: Question about enums
  • From: Michael de Haan  <email@hidden>
  • Date: Fri, 17 Jul 2015 16:14:13 -0700

Could you elaborate on the persistence aspect…as this is what I will be needing.
Thanks



> On Jul 17, 2015, at 2:04 PM, iseecolors <email@hidden> wrote:
>
> As long as you only need one customer defined enum and it is not expected to persist, than this will work.
>
>> On Jul 17, 2015, at 1:57 PM, Stephen J. Butler <email@hidden> wrote:
>>
>> Or what about this? Tested this time...
>>
>> enum Foo {
>>  case Bar
>>  case Etc
>>  case Etc_Etc
>>  case Custom(String)
>>
>>  func asString() -> String {
>>      switch self {
>>      case .Bar: return "Bar"
>>      case .Etc: return "Etc"
>>      case .Etc_Etc: return "Etc Etc"
>>      case .Custom(let value): return value
>>      }
>>  }
>>
>>  init(withString value: String) {
>>      switch value {
>>      case "Bar": self = .Bar
>>      case "Etc": self = .Etc
>>      case "Etc Etc": self = .Etc_Etc
>>      default:
>>          self = .Custom(value)
>>      }
>>
>>  }
>> }
>>
>> let a = Foo(withString: "Bar")
>> let b = Foo(withString: "Something Else")
>>
>> print("a = \(a.asString()); b = \(b.asString())")
>>
>> I guess more typing, and some room for error in the initializer, but it
>> does do away with the wrapper class.
>>
>> On Fri, Jul 17, 2015 at 3:39 PM, Quincey Morris <
>> email@hidden> wrote:
>>
>>> On Jul 17, 2015, at 13:33 , Stephen J. Butler <email@hidden>
>>> wrote:
>>>
>>>
>>> How about using a Custom option with an associated String value?
>>>
>>> enum Foo:String {
>>>
>>> case Bar = “Bar"
>>> case Etc = “Etc"
>>> case Etc_Etc = “Etc Etc"
>>> case Custom(String)
>>> }
>>>
>>>
>>> Unfortunately that won’t compile. One alternative is:
>>>
>>> enum Foo: String {
>>> case Bar = "Bar"
>>> case Etc = "Etc"
>>> case Etc_Etc = "Etc Etc"
>>> }
>>>
>>> enum FooPlus {
>>> case AFoo (Foo)
>>> case AString (String)
>>>
>>> var asString: String {
>>> switch self
>>> {
>>> case let .AFoo (foo): return foo.rawValue
>>> case let.AString (string): return string
>>> }
>>> }
>>> }
>>>
>>>
>>>
>>>
>> _______________________________________________
>>
>> Cocoa-dev mailing list (email@hidden)
>>
>> Please do not post admin requests or moderator comments to the list.
>> Contact the moderators at cocoa-dev-admins(at)lists.apple.com
>>
>> Help/Unsubscribe/Update your Subscription:
>>
>> This email sent to email@hidden
>
>
> _______________________________________________
>
> Cocoa-dev mailing list (email@hidden)
>
> Please do not post admin requests or moderator comments to the list.
> Contact the moderators at cocoa-dev-admins(at)lists.apple.com
>
> Help/Unsubscribe/Update your Subscription:
>
> This email sent to email@hidden


_______________________________________________

Cocoa-dev mailing list (email@hidden)

Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com

Help/Unsubscribe/Update your Subscription:

This email sent to email@hidden


References: 
 >Question about enums (From: Michael de Haan  <email@hidden>)
 >Re: Question about enums (From: "Stephen J. Butler" <email@hidden>)
 >Re: Question about enums (From: Quincey Morris <email@hidden>)
 >Re: Question about enums (From: "Stephen J. Butler" <email@hidden>)
 >Re: Question about enums (From: iseecolors <email@hidden>)

  • Prev by Date: Re: Safe time to add accessory view to NSSavePanel in sandboxed app?
  • Next by Date: Strange behavior of view based NSTableView with NSTextView
  • Previous by thread: Re: Question about enums
  • Next by thread: Re: Question about enums
  • Index(es):
    • Date
    • Thread