{"id":5,"date":"2015-11-28T10:07:18","date_gmt":"2015-11-28T16:07:18","guid":{"rendered":"https:\/\/chibicode.org\/?p=5"},"modified":"2015-11-28T10:07:18","modified_gmt":"2015-11-28T16:07:18","slug":"better-mogenerator-swift-templates","status":"publish","type":"post","link":"https:\/\/chibicode.org\/?p=5","title":{"rendered":"Better mogenerator Swift Templates"},"content":{"rendered":"<p>It finally happened that Core Data was required for one of my Swift 2 projects, so naturally I turned to mogenerator to produce my human and machine NSManagedObject subclasses. However, I was unsatisfied with the results of the machine file\u2026particularly, the tendency of the template to clutter up the global namespace with enums for the attributes and relationships. Here&#8217;s the default template&#8217;s output for an entity named Category with a pair of attributes and one relationship.<\/p>\n<p>[gist https:\/\/gist.github.com\/JoshuaSullivan\/dbbe909dd08a0fbadb92 file=&#8221;Old Output Snippet.swift&#8221; \/]<\/p>\n<p>As you can see, it creates a pair of public enums to enumerate the names of the attributes and relationships. This isn&#8217;t too bad for one or two entities, but gets annoying when you have dozens of entities, each adding 2-4 new global-level types.<\/p>\n<p>Moreover, I have a problem with the use of the enum type. Yes, we are enumerating the possible attributes for this entity, but absolutely nothing in the Core Data framework will accept them; it&#8217;s purely string-based. As a result, anywhere you use the enum cases, you&#8217;ll have to tack on the .rawValue accessor to get the underlying string. I figured we could do better:<\/p>\n<p>[gist https:\/\/gist.github.com\/JoshuaSullivan\/dbbe909dd08a0fbadb92 file=&#8221;New Output Snippet.swift&#8221; \/]<\/p>\n<p>The way I chose to address the first issue is to move the declarations inside the class declaration for the entity. Since Swift supports name-spacing, this means that instead of typing &#8220;CategoryAttributes.id&#8221;, you&#8217;d type &#8220;Category.Attributes.id&#8221;. This is only 1 more character and it results in only &#8220;Category&#8221; being added to the global namespace.<\/p>\n<p>The second change I made was to convert the enums to structs with each of the entries being a static let string. The structs are never meant to be instantiated, they simply provide a coherent namespace for our attribute and relationship names.<\/p>\n<p>You can get the updated template files here: <a href=\"https:\/\/gist.github.com\/JoshuaSullivan\/2057f09cc18243a3f2df\">https:\/\/gist.github.com\/JoshuaSullivan\/2057f09cc18243a3f2df<\/a><\/p>\n<p>More information about using custom mogenerator templates can be found <a href=\"http:\/\/stackoverflow.com\/questions\/3589247\/how-do-the-mogenerator-parameters-work-which-can-i-send-via-xcode\">in this Stack Overflow post.<\/a><\/p>\n<p>&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>It finally happened that Core Data was required for one of my Swift 2 projects, so naturally I turned to mogenerator to produce my human and machine NSManagedObject subclasses. However, I was unsatisfied with the results of the machine file\u2026particularly, the tendency of the template to clutter up the global namespace with enums for the &hellip; <a href=\"https:\/\/chibicode.org\/?p=5\" class=\"more-link\">Continue reading<span class=\"screen-reader-text\"> &#8220;Better mogenerator Swift Templates&#8221;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[5],"tags":[7,8,6],"class_list":["post-5","post","type-post","status-publish","format-standard","hentry","category-code","tag-core-data","tag-mogenerator","tag-swift"],"_links":{"self":[{"href":"https:\/\/chibicode.org\/index.php?rest_route=\/wp\/v2\/posts\/5","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/chibicode.org\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/chibicode.org\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/chibicode.org\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/chibicode.org\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=5"}],"version-history":[{"count":3,"href":"https:\/\/chibicode.org\/index.php?rest_route=\/wp\/v2\/posts\/5\/revisions"}],"predecessor-version":[{"id":6,"href":"https:\/\/chibicode.org\/index.php?rest_route=\/wp\/v2\/posts\/5\/revisions\/6"}],"wp:attachment":[{"href":"https:\/\/chibicode.org\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=5"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/chibicode.org\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=5"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/chibicode.org\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=5"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}