Oops… I should have called it genericTfor etc as in:
>>>>>>>>>>>>>>> func intFor(s:String) -> Int { return Int(s)! } func genericTFor<T>(s:String) -> T { return T(s)! } let intFromStr = intFor("9") let intFromStrB:Double = genericTFor("9") <<<<<<<<<<<<<<<< in other words, write a generic function that will return an Int, Double for a given string value, depending upon the input i.e. T > On Jul 26, 2015, at 3:44 PM, Alan Westbrook <void...@me.com> wrote: > > I think you want genericIntFor() to return Int instead of T > > Alan > >> On Jul 26, 2015, at 3:10 PM, Michael de Haan <m...@comcast.net> wrote: >> >> I’m writing a coreData helper which will rely on Generics. >> >> Could I get some input? >> >> I have synthesized the problem down to this somewhat nonsensical code. >> >> From Playground: >> >> >>>>>>>>>> >> >> >> func intFor(s:String) -> Int { >> >> return Int(s)! >> >> } >> >> func genericIntFor<T>(s:String) -> T { >> >> return T(s)! // T cannot be constructed because it has no accessible >> initializers. >> >> } >> >> >> >> let intFromStr = intFor("9”) // 9 >> let intFromStrB:Double = genericIntFor("9”) >> >> <<<<<<<<< >> >> >> >> The first function does pretty much what it says. >> The second tries to use Generics, with an error as shown. Now, I **thought** >> ( which is often not a good thing) that T would allow , in this case, either >> an integer or double to be returned, given a valid string input that >> represents a number, say from 0 to 9. >> Is it possible the way I wrote, or is there some fundamental >> misunderstanding of Generics? Thanks in advance. >> _______________________________________________ >> >> 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/voidref%40me.com >> >> This email sent to void...@me.com > _______________________________________________ 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