[swift-users] Instantiate Swift class from string

Matthew Davies daviesgeek at gmail.com
Thu Dec 10 11:59:29 CST 2015


I don't really like the idea of a factory function, but unfortunately that
might be the only way to do it :( However, due to my specific use case, I
don't think a factory function will work. I'm working on a framework that
will need to both instantiate the class from a string (or class type)
*and* call
methods dynamically on it. Which, I'm not sure I can do in the build tools
that are provided in the open source package. Foundation hasn't been fully
implemented and is missing a lot of the methods that would allow this to
work.

@Jens thanks for that blog post. I'll have to make sure I check back to see
what his solution is for it.




*Matthew Davies*
Junior Developer, GeoStrategies <http://geostrategies.com>
Director of Photography, OffBlock Films <http://offblockfilms.com>
209-225-3246 <209-225.3246> | 209-202-3284 | daviesgeek at gmail.com |
daviesgeek.com
<http://facebook.com/daviesgeek>  <http://us.linkedin.com/in/daviesgeek>
<http://twitter.com/daviesgeek>  <http://daviesgeek.com/feed.xml>
<http://github.com/daviesgeek>


On Thu, Dec 10, 2015 at 9:30 AM, Jan Neumüller <swift-users at swift.org>
wrote:

> Please no factory madness in Swift. This stuff is bad enough in Java -
> don’t infect Swift with it.
>
> Jan
>
> On 10.12.2015, at 18:23, Jens Alfke via swift-users <swift-users at swift.org>
> wrote:
>
>
> On Dec 10, 2015, at 7:26 AM, Harlan Haskins via swift-users <
> swift-users at swift.org> wrote:
>
> IIRC this isn’t possible because there’s no Runtime to query for
> classnames (it’s inherently unsafe anyway).
>
>
> It’s not unsafe if you specify a base class/protocol that the loaded class
> must conform to.
>
> You might want to look into a better way of doing that you’re trying to do.
>
>
> I disagree with “a better way” — “a workaround” is how I’d rephrase it.
> This kind of dynamism is often the best tool for the job, and a lot of
> Cocoa developers are frustrated by its absence in Swift. For example,
> there’s a series of blog posts from earlier this year by the highly
> respected Brent Simmons [NetNewsWire, MarsEdit, Glassboard, etc., currently
> at Omni]:
> http://inessential.com/swiftdiary
> http://inessential.com/2015/07/20/swift_diary_1_class_or_struct_from_str
>
> The workaround I’d suggest is a factory function that contains a switch
> statement that matches class names and returns newly initialized instances.
>
> —Jens
> _______________________________________________
> swift-users mailing list
> swift-users at swift.org
> https://lists.swift.org/mailman/listinfo/swift-users
>
>
>
> _______________________________________________
> swift-users mailing list
> swift-users at swift.org
> https://lists.swift.org/mailman/listinfo/swift-users
>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://lists.swift.org/pipermail/swift-users/attachments/20151210/b0eb1093/attachment.html>


More information about the swift-users mailing list