× {{alert.msg}} Never ask again
Get notified about new tutorials RECEIVE NEW TUTORIALS

concatenate enum of strings

Antonio Bello
Apr 15, 2015
<p>I presume that your enum has a raw value of type string. In that case, I would recommend creating a static function that given a enum case returns an absolute URL obtained by appending the value of a case representing a path to the value of the base url:</p> <pre><code>enum CONFIG_URLS : String { case BASE_URL = "http://www.myurl.com" case URL1 = "/some/path" static func getUrl(url: CONFIG_URLS) -&gt; String { switch url { case .BASE_URL: return BASE_URL.rawValue default: return "\(CONFIG_URLS.BASE_URL.rawValue)\(url.rawValue)" } } } println(CONFIG_URLS.getUrl(.BASE_URL)) println(CONFIG_URLS.getUrl(.URL1)) </code></pre> <p>Alternatively, the static method can be converted to a property, used in a similar way:</p> <pre><code>enum CONFIG_URLS : String { case BASE_URL = "http://www.myurl.com" case URL1 = "/some/path" var url: String { switch self { case .BASE_URL: return BASE_URL.rawValue default: return "\(CONFIG_URLS.BASE_URL.rawValue)\(self.rawValue)" } } } println(CONFIG_URLS.BASE_URL.url) println(CONFIG_URLS.URL1.url) </code></pre> <p>This tip was originally posted on <a href="http://stackoverflow.com/questions/29383555/concatenate%20enum%20of%20strings/29384853">Stack Overflow</a>.</p>
comments powered by Disqus