Re: Swift enums with raw values
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