Swift enums with raw values
Swift enums with raw values
- Subject: Swift enums with raw values
- From: Rick Mann <email@hidden>
- Date: Tue, 07 Jul 2015 00:02:02 -0700
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")
where
enum
HTTPHeader : String
{
case Accept = "Accept"
case ContentDisposition = "Content-Disposition"
case ContentLength = "Content-Length"
case ContentType = "Content-Type"
case Authorization = "Authorization"
case Date = "Date"
case IfModifiedSince = "If-Modified-Since"
case UserAgent = "User-Agent"
enum
HTTPContentType : String
{
case JSON = "application/json"
case JSONUTF8 = "application/json; charset=UTF-8"
case OctetStream = "application/octet-stream"
}
That is, I'd like to not have to explicitly call .rawValue() every time I use one of the enum cases, and I'd like the Header field name to be one enum, and the value to be any of a number of different enums. Finally, I'd like to be able to call set() with one or both parameters of type String, but I think I can accomplish that with overloading.
Unfortunately, I can't figure out how to make a set of enum types I define conform to a single type (protocol, class, or "abstract enum") such that Swift doesn't complain.
Any suggestions? Thanks.
--
Rick Mann
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