{"id":1993,"date":"2015-12-30T23:59:06","date_gmt":"2015-12-30T23:59:06","guid":{"rendered":"http:\/\/justthesam.com\/?p=1993"},"modified":"2015-12-30T23:59:06","modified_gmt":"2015-12-30T23:59:06","slug":"using-a-carthage-framework-in-a-playground","status":"publish","type":"post","link":"https:\/\/justthesam.com\/?p=1993","title":{"rendered":"Using a Carthage framework in a Playground"},"content":{"rendered":"<h2>TL;DR<\/h2>\n<p>It&#8217;s actually pretty simple, if you know what you&#8217;re doing:<\/p>\n<ul>\n<li>convert project to a workspace if it isn&#8217;t already<\/li>\n<li>add Carthage\/Checkouts\/SWXMLHash\/SWXMLHash.xcodeproj to the workspace<\/li>\n<li>move playgrounds to the workspace (not a\u00a0project)<\/li>\n<li>build the SWXMLHash project.<\/li>\n<li>import SWXMLHash successfully in your playground, as long as the target you have selected includes the SWXMLHash framework project.<\/li>\n<\/ul>\n<h2>But if you don&#8217;t know what you&#8217;re doing&#8230;<\/h2>\n<p>I struggled\u00a0to &#8220;import\u00a0SWXMLHash&#8221; in\u00a0my XCode Playground. I kept getting an angry red error for the module import.\u00a0<a href=\"https:\/\/github.com\/drmohundro\/SWXMLHash\">SWXMLHash<\/a> is a third party framework I wanted to experiment with, that makes XML parsing nice and simple (ish).<\/p>\n<p>Having figured out the trick to getting third party frameworks working in playgrounds, I thought I&#8217;d document it and the pitfalls. Really it&#8217;s pretty much just a case of following <a href=\"https:\/\/developer.apple.com\/library\/ios\/recipes\/Playground_Help\/Chapters\/ImportFramework.html\">Apple&#8217;s own\u00a0documentation<\/a>, but I still took a while to get it right, mostly due to still being on the learning curve for the ecosystem. So my instructions are more tailored to the innocent newbie.<\/p>\n<p>In my case I&#8217;m using Carthage to fetch and build SWXMLHash, so I have a copy of the framework&#8217;s project source\u00a0in my project&#8217;s Carthage\/Checkouts\/SWXMLHash directory. Which we can use later, but first&#8230;<\/p>\n<p>You need to be using a Workspace, not a Project in XCode. I had a project because that&#8217;s what XCode\u00a0gave\u00a0me when I started my new world-beating app, and I didn&#8217;t know any better. Use File &gt; Save as Workspace&#8230; to save a workspace file containing just your current project. It seems to be traditional to use the same name as the main project, so we end up with Foo.xcodeproj and Foo.xcworkspace files. From now on, always open the workspace not the project.<\/p>\n<p>Now move your playgrounds out from your project and into the workspace &#8211; i.e. up a level. I did this by deleting the playground reference from within the original project, then adding it to the workspace with the + button in the very bottom-left of the XCode UI. That button adds to whatever is selected, so ensure nothing is selected (cmd-click on the currently selected item to deselect it). There are probably other ways. Hell, maybe you can even drag them, but I didn&#8217;t try that.<\/p>\n<p>Playgrounds can only deal with Frameworks whose project is within the same workspace. If you&#8217;ve only got a .framework file I believe you can put it\u00a0in with the system frameworks in the right place on disk and it will be found, as a workaround, but I&#8217;ve got the project courtesy of Carthage so we&#8217;re OK here. Add that project (Carthage\/Checkouts\/SWXMLHash\/SWXMLHash.xcodeproj in my case) to the workspace via that bottom-left + button. Build that freshly added framework project for Mac by selecting the relevant scheme from the dropdown in the toolbar and selecting Product &gt; Build.<\/p>\n<p>Now, in your playground, you should be able to import and use that framework, but there&#8217;s one final wrinkle:\u00a0the currently selected target must include the SWXMLHash framework project. Targets that include the Carthage-built framework don&#8217;t count &#8211; it has to be the framework project that you added above. So for example, selecting the SWXMLHash target\u00a0itself works. You want the\u00a0OSX build, because that&#8217;s the variant of the framework that is used in the playground.<\/p>\n<p>It&#8217;s also probably for the best\u00a0that your playgrounds now exist in a workspace rather than in a project, cleanliness-wise. It should look something like the image below, with playground, framework project and my own project in the workspace, and the module import working correctly in the playground<\/p>\n<p><a href=\"http:\/\/20.166.29.245\/wp-content\/uploads\/2015\/12\/Screen-Shot-2015-12-30-at-23.57.44.png\" rel=\"attachment wp-att-1996\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter wp-image-1996\" src=\"http:\/\/20.166.29.245\/wp-content\/uploads\/2015\/12\/Screen-Shot-2015-12-30-at-23.57.44-300x89.png\" alt=\"Workspace setup\" width=\"500\" height=\"149\" srcset=\"https:\/\/justthesam.com\/wp-content\/uploads\/2015\/12\/Screen-Shot-2015-12-30-at-23.57.44-300x89.png 300w, https:\/\/justthesam.com\/wp-content\/uploads\/2015\/12\/Screen-Shot-2015-12-30-at-23.57.44-768x229.png 768w, https:\/\/justthesam.com\/wp-content\/uploads\/2015\/12\/Screen-Shot-2015-12-30-at-23.57.44.png 818w\" sizes=\"auto, (max-width: 500px) 100vw, 500px\" \/><\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>TL;DR It&#8217;s actually pretty simple, if you know what you&#8217;re doing: convert project to a workspace if it isn&#8217;t already add Carthage\/Checkouts\/SWXMLHash\/SWXMLHash.xcodeproj to the workspace move playgrounds to the workspace (not a\u00a0project) build the SWXMLHash project. import SWXMLHash successfully in your playground, as long as the target you have selected includes the SWXMLHash framework project. [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[22,27],"tags":[],"class_list":["post-1993","post","type-post","status-publish","format-standard","hentry","category-programming","category-swift"],"_links":{"self":[{"href":"https:\/\/justthesam.com\/index.php?rest_route=\/wp\/v2\/posts\/1993","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/justthesam.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/justthesam.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/justthesam.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/justthesam.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=1993"}],"version-history":[{"count":0,"href":"https:\/\/justthesam.com\/index.php?rest_route=\/wp\/v2\/posts\/1993\/revisions"}],"wp:attachment":[{"href":"https:\/\/justthesam.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=1993"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/justthesam.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=1993"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/justthesam.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=1993"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}