ios - concatenate enum of strings -
i have 2 enums below.
config_urls.base_url config_urls.url1
each of these enums points string.
i want create string variable concatenating base_url
, url1
. should swift code. can help? many thanks
i presume enum has raw value of type string. in case, recommend creating static function given enum case returns absolute url obtained appending value of case representing path value of base url:
enum config_urls : string { case base_url = "http://www.myurl.com" case url1 = "/some/path" static func geturl(url: config_urls) -> 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))
alternatively, static method can converted property, used in similar way:
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)
Comments
Post a Comment