{"id":8970,"date":"2014-06-13T14:03:53","date_gmt":"2014-06-13T18:03:53","guid":{"rendered":"http:\/\/mjtsai.com\/blog\/?p=8970"},"modified":"2014-06-13T14:05:43","modified_gmt":"2014-06-13T18:05:43","slug":"instantiating-classes-by-name-in-swift","status":"publish","type":"post","link":"https:\/\/mjtsai.com\/blog\/2014\/06\/13\/instantiating-classes-by-name-in-swift\/","title":{"rendered":"Instantiating Classes by Name in Swift"},"content":{"rendered":"<p><a href=\"http:\/\/ijoshsmith.com\/2014\/06\/05\/instantiating-classes-by-name-in-swift\/\">Josh Smith<\/a>:<\/p>\n<blockquote cite=\"http:\/\/ijoshsmith.com\/2014\/06\/05\/instantiating-classes-by-name-in-swift\/\"><p>In Objective-C that is easy as pie, just call <em>NSClassFromString(aClassName)<\/em> and then <em>alloc<\/em>\/<em>init<\/em> the Class it returns. However, now that Swift is destined to become the <em>lingua franca<\/em> of iOS I decided to investigate how one might implement these reusable components in Swift. Instantiating classes by name turned out to be a stumbling block, as it appears that Swift does not yet support this.<\/p>\n<p>[&#8230;]<\/p>\n<p>I was able to get an object that represents a class (named <em>AnyClass<\/em>), but there was no way to get from there to a live instance of that class. So instead, I hopped into the parallel universe of Objective-C to create an object, which then gets returned back to Swift. This is why <em>ObjectFactory<\/em> can only work with <em>NSObject<\/em> subclasses; I&rsquo;m relying on Objective-C to <em>alloc<\/em>\/<em>init<\/em> a new object, and those methods are defined by <em>NSObject<\/em>.<\/p><\/blockquote>\n<p>I <a href=\"http:\/\/mjtsai.com\/blog\/2014\/06\/04\/swift-links\/\">still<\/a> think one of the less remarked on aspects of Swift is its <a href=\"https:\/\/twitter.com\/pilky\/status\/476658642960515072\">lack of dynamism<\/a>. It does not seem like Core Data, <a href=\"http:\/\/www.libertypages.com\/clarktech\/?p=81\">Scripting Bridge<\/a>, or PyObjC could be written in Swift. Swift gives you a smarter compiler, but less flexibility at runtime. Swift is famously advertised as Objective-C without the C. Curt Clifton says:<\/p>\n<blockquote cite=\"https:\/\/twitter.com\/curtclifton\/status\/477504907075792896\"><p>Starting to fear that Swift is really Obj-C without the Smalltalk[&#8230;]<\/p><\/blockquote>","protected":false},"excerpt":{"rendered":"<p>Josh Smith: In Objective-C that is easy as pie, just call NSClassFromString(aClassName) and then alloc\/init the Class it returns. However, now that Swift is destined to become the lingua franca of iOS I decided to investigate how one might implement these reusable components in Swift. Instantiating classes by name turned out to be a stumbling [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"apple_news_api_created_at":"","apple_news_api_id":"","apple_news_api_modified_at":"","apple_news_api_revision":"","apple_news_api_share_url":"","apple_news_coverimage":0,"apple_news_coverimage_caption":"","apple_news_is_hidden":false,"apple_news_is_paid":false,"apple_news_is_preview":false,"apple_news_is_sponsored":false,"apple_news_maturity_rating":"","apple_news_metadata":"\"\"","apple_news_pullquote":"","apple_news_pullquote_position":"","apple_news_slug":"","apple_news_sections":"\"\"","apple_news_suppress_video_url":false,"apple_news_use_image_component":false,"footnotes":""},"categories":[4],"tags":[46,54,71,901],"class_list":["post-8970","post","type-post","status-publish","format-standard","hentry","category-programming-category","tag-languagedesign","tag-objective-c","tag-programming","tag-swift-programming-language"],"apple_news_notices":[],"_links":{"self":[{"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/posts\/8970","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/comments?post=8970"}],"version-history":[{"count":2,"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/posts\/8970\/revisions"}],"predecessor-version":[{"id":8972,"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/posts\/8970\/revisions\/8972"}],"wp:attachment":[{"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/media?parent=8970"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/categories?post=8970"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/tags?post=8970"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}