Nice…  thank you Marco




> On Jul 26, 2015, at 4:43 PM, Marco S Hyman <m...@snafu.org> wrote:
> 
>> func genericFor<T>(s:String) -> T {
>> 
>>   return T(s)!  // error. ’T’ cannot be constructed because it has no 
>> accessible initializers
>> }
> 
> At compile time there is no way of determining if T has an initializer that
> takes a string. You could do something like this
> 
> ----------------
> 
> protocol InitFromString {
>    init?(_ value: String)
> }
> 
> func genericFor<T: InitFromString>(s:String) -> T {
> 
>   return T(s)!
> }
> 
> ----------------
> 
> Then add extensions to the various types you want to convert to make sure
> they conform to the InitFromString protocol.
> 


_______________________________________________

Cocoa-dev mailing list (Cocoa-dev@lists.apple.com)

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:
https://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com

This email sent to arch...@mail-archive.com

Reply via email to