{"id":483,"date":"2015-03-05T11:36:14","date_gmt":"2015-03-05T10:36:14","guid":{"rendered":"http:\/\/andras.palfi.hu\/?p=483"},"modified":"2015-03-05T11:36:14","modified_gmt":"2015-03-05T10:36:14","slug":"allocate-swift-objects-from-objective-c","status":"publish","type":"post","link":"https:\/\/andras.palfi.hu\/?p=483","title":{"rendered":"Allocate Swift objects from Objective-C"},"content":{"rendered":"<p>\t\t\t\tUsing mixed projects you can face with several challenging situations. One of them is the allocation of Swift objects in ObjC code.<\/p>\n<p>The general problem that Swift objects doesn&#8217;t have an\u00a0<em>alloc<\/em> method which is used to allocate the memory in ObjC ([<em>[SwiftClass alloc] init]<\/em>).<br \/>\nTo access the Swift object you should prefix it with\u00a0<em>@objc<strong>\u00a0&#8211;\u00a0<\/strong><\/em>or subclass from NSObject.<\/p>\n<p>There are several workarounds however:<\/p>\n<ul>\n<li>Make the <em>NSObject<\/em> as base class of the Swift class. As you use mixed project already this seems to be the best solution.\n<pre class=\"lang:default decode:true\">class YourSwiftClass : NSObject<\/pre>\n<\/li>\n<li>Declare a class (static) method which returns a new instance of the object:\n<pre class=\"lang:default decode:true \">class func newInstance() -&gt; YourSwiftClass {\n    return YourSwiftClass()\n}<\/pre>\n<\/li>\n<li>Implement the\u00a0<em>alloc\u00a0<\/em>method &#8211; similar to the previous\u00a0solution\n<pre class=\"lang:default decode:true\">class func alloc() -&gt; YourSwiftClass {\n    return YourSwiftClass()\n}<\/pre>\n<\/li>\n<li>Use a kind of introspection\n<pre class=\"lang:default decode:true\">[[NSClassFromString(@\"YourProjectName.YourSwiftClass\") alloc] init]<\/pre>\n<\/li>\n<li>Declare but\u00a0<strong>not<\/strong> implement a category in the\u00a0<strong>ObjC<\/strong> code where you want to allocate the Swift object &#8211; yes, it really works\n<pre class=\"lang:default decode:true\">@interface YourSwiftClass (allocation)\n    + (instancetype)alloc;\n@end<\/pre>\n<\/li>\n<\/ul>\n","protected":false},"excerpt":{"rendered":"<p>Using mixed projects you can face with several challenging situations. One of them is the allocation of Swift objects in ObjC code. The general problem that Swift objects doesn&#8217;t have an\u00a0alloc method which is used to allocate the memory in ObjC ([[SwiftClass alloc] init]). To access the Swift object you should prefix it with\u00a0@objc\u00a0&#8211;\u00a0or subclass [&hellip;]<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"hide_page_title":"","footnotes":""},"categories":[3,4,9],"tags":[],"_links":{"self":[{"href":"https:\/\/andras.palfi.hu\/index.php?rest_route=\/wp\/v2\/posts\/483"}],"collection":[{"href":"https:\/\/andras.palfi.hu\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/andras.palfi.hu\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/andras.palfi.hu\/index.php?rest_route=\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/andras.palfi.hu\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=483"}],"version-history":[{"count":0,"href":"https:\/\/andras.palfi.hu\/index.php?rest_route=\/wp\/v2\/posts\/483\/revisions"}],"wp:attachment":[{"href":"https:\/\/andras.palfi.hu\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=483"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/andras.palfi.hu\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=483"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/andras.palfi.hu\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=483"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}