• 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: Swift enums with raw values
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Swift enums with raw values


  • Subject: Re: Swift enums with raw values
  • From: Quincey Morris <email@hidden>
  • Date: Tue, 07 Jul 2015 07:20:48 +0000

On Jul 7, 2015, at 00:02 , Rick Mann <email@hidden> wrote:
>
> I want to implement some support for HTTP Headers in enums, such that I can write this:
>
> extension
> NSMutableURLRequest
> {
> 	func
> 	set(inHeader : String, _ inVal : String)
> 	{
> 		self.setValue(inVal.rawValue, forHTTPHeaderField: inHeader.rawValue())
> 	}
> }
>
> and then call it like this:
>
> 	req.set(.ContentType, .JSON)
> 	req.set(.ContentType, "some other type")
>

Like this:

> extension NSMutableURLRequest
> {
> 	func set<T, U where T: RawRepresentable, T.RawValue == String, U: RawRepresentable, U.RawValue == String> (inHeader : T, _ inVal : U)
> 	{
> 		self.setValue (inVal.rawValue, forHTTPHeaderField: inHeader.rawValue)
> 	}
>
> }

At least, that compiles in a playground. As you say, overloading should work for pure String values of the second parameter.



_______________________________________________

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: 
 >Swift enums with raw values (From: Rick Mann <email@hidden>)

  • Prev by Date: cannot invoke 'substringToIndex' with an argument list of type '(Int)'
  • Next by Date: Re: cannot invoke 'substringToIndex' with an argument list of type '(Int)'
  • Previous by thread: Swift enums with raw values
  • Next by thread: cannot invoke 'substringToIndex' with an argument list of type '(Int)'
  • Index(es):
    • Date
    • Thread