<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom">
  <channel>
    <title>Playground on Andrew Bancroft</title>
    <link>https://www.andrewcbancroft.com/tags/playground/</link>
    <description>Recent content about iOS development with Swift in Playground  from Andrew Bancroft.</description>
    <generator>Hugo -- gohugo.io</generator>
    <language>en-us</language>
    <lastBuildDate>Sat, 06 Jul 2019 03:11:48 +0000</lastBuildDate>
    
        <atom:link href="https://www.andrewcbancroft.com/tags/playground/index.xml" rel="self" type="application/rss+xml" />
    
    
    <item>
      <title>Apple&#39;s SwiftUI Essentials Tutorial Series as Playgrounds</title>
      <link>https://www.andrewcbancroft.com/blog/ios-development/ui-work/swiftui-essentials-playgrounds/</link>
      <pubDate>Sat, 06 Jul 2019 03:11:48 +0000</pubDate>
      
      <guid>https://www.andrewcbancroft.com/blog/ios-development/ui-work/swiftui-essentials-playgrounds/</guid>
      <description>&lt;p&gt;&lt;a name=&#34;resources&#34; class=&#34;jump-target&#34;&gt;&lt;/a&gt;&lt;/p&gt;
&lt;div class=&#34;resources&#34;&gt;
&lt;div class=&#34;resources-header&#34;&gt;
Resources
&lt;/div&gt;
&lt;ul class=&#34;resources-content&#34;&gt;
&lt;li&gt;
&lt;i class=&#34;fas fa-file-code&#34;&gt;&lt;/i&gt; &lt;a href=&#34;https://github.com/andrewcbancroft/swiftui-essentials-playgrounds&#34;&gt;SwiftUI Essentials Tutorials as Playgrounds&lt;/a&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;i class=&#34;fas fa-link&#34;&gt;&lt;/i&gt; &lt;a href=&#34;https://developer.apple.com/tutorials/swiftui&#34; rel=&#34;nofollow&#34;&gt;Apple&#39;s SwiftUI tutorials&lt;/a&gt;
&lt;/li&gt;
&lt;/ul&gt;
&lt;/div&gt;
&lt;h2 id=&#34;why-playgrounds&#34;&gt;Why Playgrounds?&lt;/h2&gt;
&lt;p&gt;I&amp;rsquo;ve been making my way through &lt;a href=&#34;https://developer.apple.com/tutorials/swiftui&#34; rel=&#34;nofollow&#34;&gt;Apple&amp;rsquo;s SwiftUI tutorials&lt;/a&gt;.&lt;/p&gt;
&lt;p&gt;If you&amp;rsquo;re anything like me, you learn by tinkering with things. Change the modifiers, reposition &lt;code&gt;Text&lt;/code&gt;, use a different &lt;code&gt;Image&lt;/code&gt;, and see what happens.&lt;/p&gt;
&lt;p&gt;BUT.&lt;/p&gt;
&lt;p&gt;Without macOS Catalina, iterating quickly on these tinker sessions involves starting and stopping a simulator to see the changes.&lt;/p&gt;
&lt;p&gt;#annoying, right?&lt;/p&gt;
&lt;p&gt;&lt;em&gt;However&lt;/em&gt;&amp;hellip; since you can &lt;a href=&#34;https://www.andrewcbancroft.com/blog/ios-development/ui-work/using-swiftui-in-playground/&#34;&gt;use SwiftUI in a Playground&lt;/a&gt;, I decided to take my learning experience out of the sample Xcode projects that Apple provides, and turn them into Playgrounds.&lt;/p&gt;
&lt;p&gt;I&amp;rsquo;ve &lt;a href=&#34;https://github.com/andrewcbancroft/swiftui-essentials-playgrounds&#34;&gt;pushed them to GitHub&lt;/a&gt;, &lt;em&gt;just in case&lt;/em&gt; they&amp;rsquo;d be useful to you.&lt;/p&gt;
&lt;h2 id=&#34;repository-structure&#34;&gt;Repository Structure&lt;/h2&gt;
&lt;p&gt;Each tutorial in the Essentials series has its own completed Playground.&lt;/p&gt;
&lt;p&gt;&lt;a href=&#34;https://www.andrewcbancroft.com/blog/ios-development/ui-work/using-swiftui-in-playground/&#34;&gt;Download or clone&lt;/a&gt; the repository to experiment with the SwiftUI tutorials individually in a Playground environment.&lt;/p&gt;
&lt;p&gt;Run the playground, and &lt;a href=&#34;https://www.andrewcbancroft.com/blog/ios-development/xcode/showing-live-view-in-swift-playground/&#34;&gt;show the live preview&lt;/a&gt; to see it in action.&lt;/p&gt;
&lt;p&gt;&lt;img src=&#34;run-preview.gif&#34; alt=&#34;Run and Preview&#34;&gt;&lt;/p&gt;
&lt;h3 id=&#34;creating-and-combining-views&#34;&gt;Creating and Combining Views&lt;/h3&gt;
&lt;p&gt;All resources required for generating the SwiftUI preview have been added under the Resources folder.&lt;/p&gt;
&lt;p&gt;&lt;img src=&#34;creating-combining-views.png&#34; alt=&#34;Creating and Combining Views&#34;&gt;&lt;/p&gt;
&lt;h3 id=&#34;building-lists-and-navigation&#34;&gt;Building Lists and Navigation&lt;/h3&gt;
&lt;p&gt;All resources required for generating the SwiftUI preview have been added under the Resources folder.&lt;/p&gt;
&lt;p&gt;Helper code for loading the JSON data file and generating images in the UI at different sizes has been placed in the &lt;code&gt;Data Helpers.swift&lt;/code&gt; file.&lt;/p&gt;
&lt;p&gt;&lt;code&gt;Model Objects.swift&lt;/code&gt; contains the code for the &lt;code&gt;Landmark&lt;/code&gt; and &lt;code&gt;Coordinates&lt;/code&gt; types.&lt;/p&gt;
&lt;p&gt;&lt;img src=&#34;building-lists-and-navigation.gif&#34; alt=&#34;Building Lists and Navigation&#34;&gt;&lt;/p&gt;
&lt;h3 id=&#34;handling-user-input&#34;&gt;Handling User Input&lt;/h3&gt;
&lt;p&gt;All resources required for generating the SwiftUI preview have been added under the Resources folder.&lt;/p&gt;
&lt;p&gt;Helper code for loading the JSON data file &lt;strong&gt;including the new isFavorite property&lt;/strong&gt;, and generating images in the UI at different sizes has been placed in the &lt;code&gt;Data Helpers.swift&lt;/code&gt; file.&lt;/p&gt;
&lt;p&gt;&lt;code&gt;Model Objects.swift&lt;/code&gt; contains the code for the &lt;code&gt;Landmark&lt;/code&gt; and &lt;code&gt;Coordinates&lt;/code&gt; types.  It also adds the &lt;code&gt;UserData&lt;/code&gt; bindable object.&lt;/p&gt;
&lt;p&gt;&lt;img src=&#34;handling-user-input.gif&#34; alt=&#34;Handling User Input&#34;&gt;&lt;/p&gt;
</description>
    </item>
    
    <item>
      <title>Using SwiftUI in a Playground</title>
      <link>https://www.andrewcbancroft.com/blog/ios-development/ui-work/using-swiftui-in-playground/</link>
      <pubDate>Thu, 13 Jun 2019 04:00:18 +0000</pubDate>
      
      <guid>https://www.andrewcbancroft.com/blog/ios-development/ui-work/using-swiftui-in-playground/</guid>
      <description>&lt;p&gt;If you&amp;rsquo;re looking for a quicker way to iterate while you&amp;rsquo;re building SwiftUI views, and you don&amp;rsquo;t have macOS Catalina installed, you might enjoy the Playground experience more than you enjoy pressing &lt;code&gt;command + R&lt;/code&gt; every time you make a change and want to see it in the user interface.&lt;/p&gt;
&lt;p&gt;How do you do it?  Here&amp;rsquo;s a code snippet to copy-paste into a Playground to get you going.&lt;/p&gt;
&lt;div class=&#34;highlight&#34;&gt;&lt;pre tabindex=&#34;0&#34; class=&#34;chroma&#34;&gt;&lt;code class=&#34;language-swift&#34; data-lang=&#34;swift&#34;&gt;&lt;span class=&#34;line&#34;&gt;&lt;span class=&#34;ln&#34;&gt; 1&lt;/span&gt;&lt;span class=&#34;cl&#34;&gt;&lt;span class=&#34;kd&#34;&gt;import&lt;/span&gt; &lt;span class=&#34;nc&#34;&gt;UIKit&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class=&#34;line hl&#34;&gt;&lt;span class=&#34;ln&#34;&gt; 2&lt;/span&gt;&lt;span class=&#34;cl&#34;&gt;&lt;span class=&#34;kd&#34;&gt;import&lt;/span&gt; &lt;span class=&#34;nc&#34;&gt;PlaygroundSupport&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class=&#34;line hl&#34;&gt;&lt;span class=&#34;ln&#34;&gt; 3&lt;/span&gt;&lt;span class=&#34;cl&#34;&gt;&lt;span class=&#34;kd&#34;&gt;import&lt;/span&gt; &lt;span class=&#34;nc&#34;&gt;SwiftUI&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class=&#34;line&#34;&gt;&lt;span class=&#34;ln&#34;&gt; 4&lt;/span&gt;&lt;span class=&#34;cl&#34;&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class=&#34;line&#34;&gt;&lt;span class=&#34;ln&#34;&gt; 5&lt;/span&gt;&lt;span class=&#34;cl&#34;&gt;&lt;span class=&#34;c1&#34;&gt;// Make a SwiftUI view&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class=&#34;line&#34;&gt;&lt;span class=&#34;ln&#34;&gt; 6&lt;/span&gt;&lt;span class=&#34;cl&#34;&gt;&lt;span class=&#34;kd&#34;&gt;struct&lt;/span&gt; &lt;span class=&#34;nc&#34;&gt;ContentView&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;:&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;View&lt;/span&gt; &lt;span class=&#34;p&#34;&gt;{&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class=&#34;line&#34;&gt;&lt;span class=&#34;ln&#34;&gt; 7&lt;/span&gt;&lt;span class=&#34;cl&#34;&gt;    &lt;span class=&#34;kd&#34;&gt;var&lt;/span&gt; &lt;span class=&#34;nv&#34;&gt;body&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;:&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;some&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;View&lt;/span&gt; &lt;span class=&#34;p&#34;&gt;{&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class=&#34;line&#34;&gt;&lt;span class=&#34;ln&#34;&gt; 8&lt;/span&gt;&lt;span class=&#34;cl&#34;&gt;        &lt;span class=&#34;n&#34;&gt;Text&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;(&lt;/span&gt;&lt;span class=&#34;s&#34;&gt;&amp;#34;SwiftUI in a Playground!&amp;#34;&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;)&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class=&#34;line&#34;&gt;&lt;span class=&#34;ln&#34;&gt; 9&lt;/span&gt;&lt;span class=&#34;cl&#34;&gt;    &lt;span class=&#34;p&#34;&gt;}&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class=&#34;line&#34;&gt;&lt;span class=&#34;ln&#34;&gt;10&lt;/span&gt;&lt;span class=&#34;cl&#34;&gt;&lt;span class=&#34;p&#34;&gt;}&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class=&#34;line&#34;&gt;&lt;span class=&#34;ln&#34;&gt;11&lt;/span&gt;&lt;span class=&#34;cl&#34;&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class=&#34;line&#34;&gt;&lt;span class=&#34;ln&#34;&gt;12&lt;/span&gt;&lt;span class=&#34;cl&#34;&gt;&lt;span class=&#34;c1&#34;&gt;// Make a UIHostingController&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class=&#34;line hl&#34;&gt;&lt;span class=&#34;ln&#34;&gt;13&lt;/span&gt;&lt;span class=&#34;cl&#34;&gt;&lt;span class=&#34;kd&#34;&gt;let&lt;/span&gt; &lt;span class=&#34;nv&#34;&gt;viewController&lt;/span&gt; &lt;span class=&#34;p&#34;&gt;=&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;UIHostingController&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;(&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;rootView&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;:&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;ContentView&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;())&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class=&#34;line&#34;&gt;&lt;span class=&#34;ln&#34;&gt;14&lt;/span&gt;&lt;span class=&#34;cl&#34;&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class=&#34;line&#34;&gt;&lt;span class=&#34;ln&#34;&gt;15&lt;/span&gt;&lt;span class=&#34;cl&#34;&gt;&lt;span class=&#34;c1&#34;&gt;// Assign it to the playground&amp;#39;s liveView&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class=&#34;line hl&#34;&gt;&lt;span class=&#34;ln&#34;&gt;16&lt;/span&gt;&lt;span class=&#34;cl&#34;&gt;&lt;span class=&#34;n&#34;&gt;PlaygroundPage&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;.&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;current&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;.&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;liveView&lt;/span&gt; &lt;span class=&#34;p&#34;&gt;=&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;viewController&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class=&#34;line&#34;&gt;&lt;span class=&#34;ln&#34;&gt;17&lt;/span&gt;&lt;span class=&#34;cl&#34;&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class=&#34;line&#34;&gt;&lt;span class=&#34;ln&#34;&gt;18&lt;/span&gt;&lt;span class=&#34;cl&#34;&gt;&lt;span class=&#34;c1&#34;&gt;// RUN!&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;&lt;img src=&#34;result.png&#34; alt=&#34;Result&#34;&gt;&lt;/p&gt;
</description>
    </item>
    
    <item>
      <title>Showing the Live View in a Swift Playground</title>
      <link>https://www.andrewcbancroft.com/blog/ios-development/xcode/showing-live-view-in-swift-playground/</link>
      <pubDate>Tue, 28 May 2019 05:06:19 +0000</pubDate>
      
      <guid>https://www.andrewcbancroft.com/blog/ios-development/xcode/showing-live-view-in-swift-playground/</guid>
      <description>&lt;h2 id=&#34;wheres-the-live-view&#34;&gt;Where&amp;rsquo;s the Live View?&lt;/h2&gt;
&lt;p&gt;So you&amp;rsquo;ve created a new Single View playground from the template picker&amp;hellip;&lt;/p&gt;
&lt;p&gt;&lt;img src=&#34;playground-template-selection.png&#34; alt=&#34;Playground Template Selection&#34;&gt;&lt;/p&gt;
&lt;p&gt;You run the playground right out of the gate to see what you&amp;rsquo;ve got&amp;hellip;&lt;/p&gt;
&lt;p&gt;&lt;img src=&#34;run-playground.gif&#34; alt=&#34;Run Playground&#34;&gt;&lt;/p&gt;
&lt;p&gt;But&amp;hellip; Where&amp;rsquo;s the Live View?&lt;/p&gt;
&lt;p&gt;&lt;img src=&#34;wheres-the-live-view.png&#34; alt=&#34;Where&amp;rsquo;s the Live View?&#34;&gt;&lt;/p&gt;
&lt;h2 id=&#34;showing-the-live-view&#34;&gt;Showing the Live View&lt;/h2&gt;
&lt;p&gt;To get the live view to show up, you need to &lt;strong&gt;open the Assitant Editor&lt;/strong&gt;.&lt;/p&gt;
&lt;p&gt;You can do this by clicking &lt;code&gt;View&lt;/code&gt; - &lt;code&gt;Assistant Editor&lt;/code&gt; - &lt;code&gt;Show Assistant Editor&lt;/code&gt;.&lt;/p&gt;
&lt;p&gt;Or you can press &lt;code&gt;option&lt;/code&gt; + &lt;code&gt;command&lt;/code&gt; + &lt;code&gt;return&lt;/code&gt; to show it.&lt;/p&gt;
&lt;p&gt;&lt;img src=&#34;show-assistant-editor.png&#34; alt=&#34;Show the Assistant Editor&#34;&gt;&lt;/p&gt;
&lt;p&gt;And we&amp;rsquo;re off to play!&lt;/p&gt;
&lt;p&gt;&lt;img src=&#34;live-view-shows.png&#34; alt=&#34;Live View Shows&#34;&gt;&lt;/p&gt;
</description>
    </item>
    
    <item>
      <title>Using a Core Data Model in Swift Playgrounds</title>
      <link>https://www.andrewcbancroft.com/2016/07/10/using-a-core-data-model-in-swift-playgrounds/</link>
      <pubDate>Sun, 10 Jul 2016 20:48:54 +0000</pubDate>
      
      <guid>https://www.andrewcbancroft.com/2016/07/10/using-a-core-data-model-in-swift-playgrounds/</guid>
      <description>&lt;p&gt;Did you know that you can tinker with Core Data inside of Swift playgrounds in Xcode? You can!&lt;/p&gt;
&lt;p&gt;&lt;a href=&#34;https://twitter.com/JCubedApps&#34;&gt;Jeremiah Jessel&lt;/a&gt;, author at &lt;a href=&#34;http://www.learncoredata.com&#34;&gt;http://www.learncoredata.com&lt;/a&gt;, wrote up an article in 2015 &lt;a href=&#34;http://www.learncoredata.com/core-data-and-playgrounds/&#34;&gt;detailing how you can use the Core Data framework inside a playground&lt;/a&gt;. He shows how you can do everything from setting up the Core Data stack, to creating NSManagedObjects programmatically in code. Great stuff!&lt;/p&gt;
&lt;p&gt;After I read his guide, I got to thinking: &lt;em&gt;I wonder&lt;/em&gt; if you can take an .xcdatamodeld file created with Xcode&amp;rsquo;s Data Model designer and use &lt;em&gt;it&lt;/em&gt; in a Playground….&lt;/p&gt;
&lt;p&gt;The short answer is, &lt;strong&gt;kinda&lt;/strong&gt;. You can&amp;rsquo;t use the .xcdatamodeld file (at least, I couldn&amp;rsquo;t find a way), BUT, you &lt;em&gt;can&lt;/em&gt; use the &lt;em&gt;compiled&lt;/em&gt; &amp;ldquo;momd” file that gets created when you build your app.&lt;/p&gt;
&lt;p&gt;&lt;a name=&#34;limitations&#34; class=&#34;jump-target&#34;&gt;&lt;/a&gt;&lt;/p&gt;
&lt;h1 id=&#34;limitations&#34;&gt;Limitations&lt;/h1&gt;
&lt;p&gt;There&amp;rsquo;s at least two limitations / caveats I&amp;rsquo;ve come across as I&amp;rsquo;ve been playing with this concept:&lt;/p&gt;
&lt;h2 id=&#34;no-nsmanagedobject-subclasses&#34;&gt;No NSManagedObject subclasses&lt;/h2&gt;
&lt;p&gt;While you can create instances of the Entities in the model, if you&amp;rsquo;ve created &lt;code&gt;NSManagedObject&lt;/code&gt; subclasses for your Entities, you won&amp;rsquo;t be able to use those in the playground. You&amp;rsquo;d have to resort back to setting properties on your &lt;code&gt;NSManagedObject&lt;/code&gt; instances using &lt;code&gt;setValue(_: forKey:)&lt;/code&gt;.&lt;/p&gt;
&lt;p&gt;But this is a minor drawback, especially if you&amp;rsquo;re just wanting to tinker.&lt;/p&gt;
&lt;h2 id=&#34;model-updates&#34;&gt;Model updates&lt;/h2&gt;
&lt;p&gt;After you read the &lt;a href=&#34;#walkthrough&#34;&gt;walkthrough&lt;/a&gt;, you&amp;rsquo;ll know how to get the model into your playground.&lt;/p&gt;
&lt;p&gt;Here&amp;rsquo;s the deal though: If you ever make &lt;em&gt;changes&lt;/em&gt; to your model, you&amp;rsquo;ll need to go through the steps necessary to &lt;em&gt;re&lt;/em&gt;-add a freshly-compiled model to the playground&amp;rsquo;s Resources folder that includes the changes. This is because resources that are added to a playground are &lt;em&gt;copied&lt;/em&gt;, not referenced.&lt;/p&gt;
&lt;p&gt;I don&amp;rsquo;t think that&amp;rsquo;s a terrible draw-back, especially once you know how to do it.&lt;/p&gt;
&lt;p&gt;So how do you do it? Here&amp;rsquo;s how:&lt;/p&gt;
&lt;p&gt;&lt;a name=&#34;walkthrough&#34; class=&#34;jump-target&#34;&gt;&lt;/a&gt;&lt;/p&gt;
&lt;h1 id=&#34;walkthrough&#34;&gt;Walkthrough&lt;/h1&gt;
&lt;p&gt;Get started by adding a Data Model to your project. If you&amp;rsquo;ve got a project already going that uses Core Data, you probably already have a .xcdatamodeld file in your project. If you don&amp;rsquo;t, though, one is easily add-able from the File menu:&lt;/p&gt;
&lt;p&gt;&lt;a name=&#34;add-data-model&#34; class=&#34;jump-target&#34;&gt;&lt;/a&gt;&lt;/p&gt;
&lt;h2 id=&#34;add-data-model-file-unless-you-already-have-one&#34;&gt;Add Data Model file (unless you already have one)&lt;/h2&gt;
&lt;p&gt;File -&amp;gt; New -&amp;gt; File…&lt;br&gt;
&lt;a href=&#34;https://www.andrewcbancroft.com/wp-content/uploads/2016/07/new-model.png&#34;&gt;&lt;img src=&#34;https://www.andrewcbancroft.com/wp-content/uploads/2016/07/new-model.png&#34; alt=&#34;New Data Model&#34; width=&#34;728&#34; height=&#34;516&#34; class=&#34;alignnone size-full wp-image-12999&#34; srcset=&#34;https://www.andrewcbancroft.com/wp-content/uploads/2016/07/new-model.png 728w, https://www.andrewcbancroft.com/wp-content/uploads/2016/07/new-model-300x213.png 300w&#34; sizes=&#34;(max-width: 728px) 100vw, 728px&#34; /&gt;&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;For my &amp;ldquo;smoke test”, just to see if it was possible, I left the default value for the model name as &amp;ldquo;Model.xcdatamodeld”.&lt;/p&gt;
&lt;p&gt;&lt;a name=&#34;add-entity&#34; class=&#34;jump-target&#34;&gt;&lt;/a&gt;&lt;/p&gt;
&lt;h2 id=&#34;add-entity-with-attribute&#34;&gt;Add entity with attribute&lt;/h2&gt;
&lt;p&gt;Once I had the data model added to the project, I went in and added an entity (named &amp;ldquo;Entity”) with an attribute (named &amp;ldquo;attribute” of type Integer 16):&lt;/p&gt;
&lt;p&gt;&lt;a href=&#34;https://www.andrewcbancroft.com/wp-content/uploads/2016/07/add-entity-and-attributes.png&#34;&gt;&lt;img src=&#34;https://www.andrewcbancroft.com/wp-content/uploads/2016/07/add-entity-and-attributes-1024x653.png&#34; alt=&#34;Add an entity with an attribute.&#34; width=&#34;1024&#34; height=&#34;653&#34; class=&#34;alignnone size-large wp-image-13003&#34; srcset=&#34;https://www.andrewcbancroft.com/wp-content/uploads/2016/07/add-entity-and-attributes-1024x653.png 1024w, https://www.andrewcbancroft.com/wp-content/uploads/2016/07/add-entity-and-attributes-300x191.png 300w, https://www.andrewcbancroft.com/wp-content/uploads/2016/07/add-entity-and-attributes.png 1129w&#34; sizes=&#34;(max-width: 1024px) 100vw, 1024px&#34; /&gt;&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;&lt;a name=&#34;add-playground&#34; class=&#34;jump-target&#34;&gt;&lt;/a&gt;&lt;/p&gt;
&lt;h2 id=&#34;add-a-playground&#34;&gt;Add a playground&lt;/h2&gt;
&lt;p&gt;Next up, I added a new playground to my project:&lt;/p&gt;
&lt;p&gt;File -&amp;gt; New -&amp;gt; Playground…&lt;br&gt;
&lt;a href=&#34;https://www.andrewcbancroft.com/wp-content/uploads/2016/07/new-playground.png&#34;&gt;&lt;img src=&#34;https://www.andrewcbancroft.com/wp-content/uploads/2016/07/new-playground.png&#34; alt=&#34;Add new playground&#34; width=&#34;877&#34; height=&#34;396&#34; class=&#34;alignnone size-full wp-image-13006&#34; srcset=&#34;https://www.andrewcbancroft.com/wp-content/uploads/2016/07/new-playground.png 877w, https://www.andrewcbancroft.com/wp-content/uploads/2016/07/new-playground-300x135.png 300w&#34; sizes=&#34;(max-width: 877px) 100vw, 877px&#34; /&gt;&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;&lt;a name=&#34;build-project&#34; class=&#34;jump-target&#34;&gt;&lt;/a&gt;&lt;/p&gt;
&lt;h2 id=&#34;build-project-locate-momd8221-file&#34;&gt;Build project; Locate &amp;ldquo;momd” file&lt;/h2&gt;
&lt;p&gt;With a playground and a data model has some structure to it, I built the project (CMD + B) so that the .xcdatamodeld file would be compiled into an &amp;ldquo;momd” file. It&amp;rsquo;s the &lt;em&gt;momd&lt;/em&gt; file that needs to be added to the playground as a resource.&lt;/p&gt;
&lt;p&gt;To find the &amp;ldquo;momd” file, expand &amp;ldquo;Products” in your project navigator, right-click the .app file, and click &amp;ldquo;Show in Finder”:&lt;br&gt;
&lt;a href=&#34;https://www.andrewcbancroft.com/wp-content/uploads/2016/07/show-product-in-finder.png&#34;&gt;&lt;img src=&#34;https://www.andrewcbancroft.com/wp-content/uploads/2016/07/show-product-in-finder.png&#34; alt=&#34;Show product in finder&#34; width=&#34;620&#34; height=&#34;515&#34; class=&#34;alignnone size-full wp-image-13008&#34; srcset=&#34;https://www.andrewcbancroft.com/wp-content/uploads/2016/07/show-product-in-finder.png 620w, https://www.andrewcbancroft.com/wp-content/uploads/2016/07/show-product-in-finder-300x249.png 300w&#34; sizes=&#34;(max-width: 620px) 100vw, 620px&#34; /&gt;&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;&lt;a name=&#34;show-app-contents&#34; class=&#34;jump-target&#34;&gt;&lt;/a&gt;&lt;/p&gt;
&lt;h2 id=&#34;show-app-package-contents&#34;&gt;Show .app package contents&lt;/h2&gt;
&lt;p&gt;In the finder window, right-click the .app file, and click &amp;ldquo;Show package contents”:&lt;br&gt;
&lt;a href=&#34;https://www.andrewcbancroft.com/wp-content/uploads/2016/07/show-package-contents.png&#34;&gt;&lt;img src=&#34;https://www.andrewcbancroft.com/wp-content/uploads/2016/07/show-package-contents.png&#34; alt=&#34;Show package contents&#34; width=&#34;749&#34; height=&#34;488&#34; class=&#34;alignnone size-full wp-image-13010&#34; srcset=&#34;https://www.andrewcbancroft.com/wp-content/uploads/2016/07/show-package-contents.png 749w, https://www.andrewcbancroft.com/wp-content/uploads/2016/07/show-package-contents-300x195.png 300w&#34; sizes=&#34;(max-width: 749px) 100vw, 749px&#34; /&gt;&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;&lt;a name=&#34;drag-to-resources&#34; class=&#34;jump-target&#34;&gt;&lt;/a&gt;&lt;/p&gt;
&lt;h2 id=&#34;drag-momd8221-file-from-finder-to-playground-resources-folder&#34;&gt;Drag &amp;ldquo;momd” file from Finder to playground Resources folder&lt;/h2&gt;
&lt;p&gt;&lt;a href=&#34;https://www.andrewcbancroft.com/wp-content/uploads/2016/07/locate-momd-file.png&#34;&gt;&lt;img src=&#34;https://www.andrewcbancroft.com/wp-content/uploads/2016/07/locate-momd-file.png&#34; alt=&#34;Locate &amp;quot;momd&amp;quot; file&#34; width=&#34;549&#34; height=&#34;304&#34; class=&#34;alignnone size-full wp-image-13013&#34; srcset=&#34;https://www.andrewcbancroft.com/wp-content/uploads/2016/07/locate-momd-file.png 549w, https://www.andrewcbancroft.com/wp-content/uploads/2016/07/locate-momd-file-300x166.png 300w&#34; sizes=&#34;(max-width: 549px) 100vw, 549px&#34; /&gt;&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;&lt;a href=&#34;https://www.andrewcbancroft.com/wp-content/uploads/2016/07/drag-momd-to-resources.png&#34;&gt;&lt;img src=&#34;https://www.andrewcbancroft.com/wp-content/uploads/2016/07/drag-momd-to-resources-1024x447.png&#34; alt=&#34;Drag &amp;quot;momd&amp;quot; to Resources&#34; width=&#34;1024&#34; height=&#34;447&#34; class=&#34;alignnone size-large wp-image-13012&#34; srcset=&#34;https://www.andrewcbancroft.com/wp-content/uploads/2016/07/drag-momd-to-resources-1024x447.png 1024w, https://www.andrewcbancroft.com/wp-content/uploads/2016/07/drag-momd-to-resources-300x131.png 300w, https://www.andrewcbancroft.com/wp-content/uploads/2016/07/drag-momd-to-resources.png 1029w&#34; sizes=&#34;(max-width: 1024px) 100vw, 1024px&#34; /&gt;&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;&lt;a name=&#34;write-code&#34; class=&#34;jump-target&#34;&gt;&lt;/a&gt;&lt;/p&gt;
&lt;h2 id=&#34;write-core-data-code-to-use-model&#34;&gt;Write Core Data code to use model&lt;/h2&gt;
&lt;p&gt;That&amp;rsquo;s it! Now that the &amp;ldquo;momd” file is in the playground&amp;rsquo;s Resources folder, you&amp;rsquo;re set to write code against it. You can insert &lt;code&gt;NSManagedObject&lt;/code&gt; instances, perform fetch requests, etc. Here&amp;rsquo;s an example that I wrote up:&lt;/p&gt;
&lt;p&gt;&lt;div class=&#34;highlight&#34;&gt;&lt;div class=&#34;chroma&#34;&gt;
&lt;table class=&#34;lntable&#34;&gt;&lt;tr&gt;&lt;td class=&#34;lntd&#34;&gt;
&lt;pre tabindex=&#34;0&#34; class=&#34;chroma&#34;&gt;&lt;code&gt;&lt;span class=&#34;lnt&#34;&gt; 1
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt; 2
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt; 3
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt; 4
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt; 5
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt; 6
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt; 7
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt; 8
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt; 9
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt;10
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt;11
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt;12
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt;13
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt;14
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt;15
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt;16
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt;17
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt;18
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt;19
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt;20
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt;21
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt;22
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt;23
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt;24
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt;25
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt;26
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt;27
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt;28
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt;29
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt;30
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt;31
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt;32
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/td&gt;
&lt;td class=&#34;lntd&#34;&gt;
&lt;pre tabindex=&#34;0&#34; class=&#34;chroma&#34;&gt;&lt;code class=&#34;language-swift&#34; data-lang=&#34;swift&#34;&gt;&lt;span class=&#34;line&#34;&gt;&lt;span class=&#34;cl&#34;&gt;&lt;span class=&#34;kd&#34;&gt;import&lt;/span&gt; &lt;span class=&#34;nc&#34;&gt;UIKit&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class=&#34;line&#34;&gt;&lt;span class=&#34;cl&#34;&gt;&lt;span class=&#34;kd&#34;&gt;import&lt;/span&gt; &lt;span class=&#34;nc&#34;&gt;CoreData&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class=&#34;line&#34;&gt;&lt;span class=&#34;cl&#34;&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class=&#34;line&#34;&gt;&lt;span class=&#34;cl&#34;&gt;&lt;span class=&#34;c1&#34;&gt;// Core Data Stack Setup for In-Memory Store&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class=&#34;line&#34;&gt;&lt;span class=&#34;cl&#34;&gt;&lt;span class=&#34;kd&#34;&gt;public&lt;/span&gt; &lt;span class=&#34;kd&#34;&gt;func&lt;/span&gt; &lt;span class=&#34;nf&#34;&gt;createMainContext&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;()&lt;/span&gt; &lt;span class=&#34;p&#34;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;NSManagedObjectContext&lt;/span&gt; &lt;span class=&#34;p&#34;&gt;{&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class=&#34;line&#34;&gt;&lt;span class=&#34;cl&#34;&gt;    
&lt;/span&gt;&lt;/span&gt;&lt;span class=&#34;line&#34;&gt;&lt;span class=&#34;cl&#34;&gt;    &lt;span class=&#34;c1&#34;&gt;// Replace &amp;#34;Model&amp;#34; with the name of your model&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class=&#34;line&#34;&gt;&lt;span class=&#34;cl&#34;&gt;    &lt;span class=&#34;kd&#34;&gt;let&lt;/span&gt; &lt;span class=&#34;nv&#34;&gt;modelUrl&lt;/span&gt; &lt;span class=&#34;p&#34;&gt;=&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;NSBundle&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;.&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;mainBundle&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;().&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;URLForResource&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;(&lt;/span&gt;&lt;span class=&#34;s&#34;&gt;&amp;#34;Model&amp;#34;&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;,&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;withExtension&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;:&lt;/span&gt; &lt;span class=&#34;s&#34;&gt;&amp;#34;momd&amp;#34;&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;)&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class=&#34;line&#34;&gt;&lt;span class=&#34;cl&#34;&gt;    &lt;span class=&#34;k&#34;&gt;guard&lt;/span&gt; &lt;span class=&#34;kd&#34;&gt;let&lt;/span&gt; &lt;span class=&#34;nv&#34;&gt;model&lt;/span&gt; &lt;span class=&#34;p&#34;&gt;=&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;NSManagedObjectModel&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;.&lt;/span&gt;&lt;span class=&#34;kd&#34;&gt;init&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;(&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;contentsOfurl&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;:&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;modelUrl&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;!)&lt;/span&gt; &lt;span class=&#34;k&#34;&gt;else&lt;/span&gt; &lt;span class=&#34;p&#34;&gt;{&lt;/span&gt; &lt;span class=&#34;bp&#34;&gt;fatalError&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;(&lt;/span&gt;&lt;span class=&#34;s&#34;&gt;&amp;#34;model not found&amp;#34;&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;)&lt;/span&gt; &lt;span class=&#34;p&#34;&gt;}&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class=&#34;line&#34;&gt;&lt;span class=&#34;cl&#34;&gt;    
&lt;/span&gt;&lt;/span&gt;&lt;span class=&#34;line&#34;&gt;&lt;span class=&#34;cl&#34;&gt;    &lt;span class=&#34;kd&#34;&gt;let&lt;/span&gt; &lt;span class=&#34;nv&#34;&gt;psc&lt;/span&gt; &lt;span class=&#34;p&#34;&gt;=&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;NSPersistentStoreCoordinator&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;(&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;managedObjectModel&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;:&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;model&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;)&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class=&#34;line&#34;&gt;&lt;span class=&#34;cl&#34;&gt;    &lt;span class=&#34;k&#34;&gt;try&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;!&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;psc&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;.&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;addPersistentStoreWithType&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;(&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;NSInMemoryStoreType&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;,&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;configuration&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;:&lt;/span&gt; &lt;span class=&#34;kc&#34;&gt;nil&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;,&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;URL&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;:&lt;/span&gt; &lt;span class=&#34;kc&#34;&gt;nil&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;,&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;options&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;:&lt;/span&gt; &lt;span class=&#34;kc&#34;&gt;nil&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;)&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class=&#34;line&#34;&gt;&lt;span class=&#34;cl&#34;&gt;    
&lt;/span&gt;&lt;/span&gt;&lt;span class=&#34;line&#34;&gt;&lt;span class=&#34;cl&#34;&gt;    &lt;span class=&#34;kd&#34;&gt;let&lt;/span&gt; &lt;span class=&#34;nv&#34;&gt;context&lt;/span&gt; &lt;span class=&#34;p&#34;&gt;=&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;NSManagedObjectContext&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;(&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;concurrencyType&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;:&lt;/span&gt; &lt;span class=&#34;p&#34;&gt;.&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;MainQueueConcurrencyType&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;)&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class=&#34;line&#34;&gt;&lt;span class=&#34;cl&#34;&gt;    &lt;span class=&#34;n&#34;&gt;context&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;.&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;persistentStoreCoordinator&lt;/span&gt; &lt;span class=&#34;p&#34;&gt;=&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;psc&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class=&#34;line&#34;&gt;&lt;span class=&#34;cl&#34;&gt;    
&lt;/span&gt;&lt;/span&gt;&lt;span class=&#34;line&#34;&gt;&lt;span class=&#34;cl&#34;&gt;    &lt;span class=&#34;k&#34;&gt;return&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;context&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class=&#34;line&#34;&gt;&lt;span class=&#34;cl&#34;&gt;&lt;span class=&#34;p&#34;&gt;}&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class=&#34;line&#34;&gt;&lt;span class=&#34;cl&#34;&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class=&#34;line&#34;&gt;&lt;span class=&#34;cl&#34;&gt;&lt;span class=&#34;kd&#34;&gt;let&lt;/span&gt; &lt;span class=&#34;nv&#34;&gt;context&lt;/span&gt; &lt;span class=&#34;p&#34;&gt;=&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;createMainContext&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;()&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class=&#34;line&#34;&gt;&lt;span class=&#34;cl&#34;&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class=&#34;line&#34;&gt;&lt;span class=&#34;cl&#34;&gt;&lt;span class=&#34;c1&#34;&gt;// Insert a new Entity&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class=&#34;line&#34;&gt;&lt;span class=&#34;cl&#34;&gt;&lt;span class=&#34;kd&#34;&gt;let&lt;/span&gt; &lt;span class=&#34;nv&#34;&gt;ent&lt;/span&gt; &lt;span class=&#34;p&#34;&gt;=&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;NSEntityDescription&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;.&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;insertNewObjectForEntityForName&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;(&lt;/span&gt;&lt;span class=&#34;s&#34;&gt;&amp;#34;Entity&amp;#34;&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;,&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;inManagedObjectContext&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;:&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;context&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;)&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class=&#34;line&#34;&gt;&lt;span class=&#34;cl&#34;&gt;&lt;span class=&#34;n&#34;&gt;ent&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;.&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;setValue&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;(&lt;/span&gt;&lt;span class=&#34;mi&#34;&gt;42&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;,&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;forKey&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;:&lt;/span&gt; &lt;span class=&#34;s&#34;&gt;&amp;#34;attribute&amp;#34;&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;)&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class=&#34;line&#34;&gt;&lt;span class=&#34;cl&#34;&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class=&#34;line&#34;&gt;&lt;span class=&#34;cl&#34;&gt;&lt;span class=&#34;k&#34;&gt;try&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;!&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;context&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;.&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;save&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;()&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class=&#34;line&#34;&gt;&lt;span class=&#34;cl&#34;&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class=&#34;line&#34;&gt;&lt;span class=&#34;cl&#34;&gt;&lt;span class=&#34;c1&#34;&gt;// Perform a fetch request&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class=&#34;line&#34;&gt;&lt;span class=&#34;cl&#34;&gt;&lt;span class=&#34;kd&#34;&gt;let&lt;/span&gt; &lt;span class=&#34;nv&#34;&gt;fr&lt;/span&gt; &lt;span class=&#34;p&#34;&gt;=&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;NSFetchRequest&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;(&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;entityName&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;:&lt;/span&gt; &lt;span class=&#34;s&#34;&gt;&amp;#34;Entity&amp;#34;&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;)&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class=&#34;line&#34;&gt;&lt;span class=&#34;cl&#34;&gt;&lt;span class=&#34;kd&#34;&gt;let&lt;/span&gt; &lt;span class=&#34;nv&#34;&gt;result&lt;/span&gt; &lt;span class=&#34;p&#34;&gt;=&lt;/span&gt; &lt;span class=&#34;k&#34;&gt;try&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;!&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;context&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;.&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;executeFetchRequest&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;(&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;fr&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;)&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class=&#34;line&#34;&gt;&lt;span class=&#34;cl&#34;&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class=&#34;line&#34;&gt;&lt;span class=&#34;cl&#34;&gt;&lt;span class=&#34;bp&#34;&gt;print&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;(&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;result&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;)&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/td&gt;&lt;/tr&gt;&lt;/table&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;a href=&#34;https://www.andrewcbancroft.com/wp-content/uploads/2016/07/printed-result.png&#34;&gt;&lt;img src=&#34;https://www.andrewcbancroft.com/wp-content/uploads/2016/07/printed-result.png&#34; alt=&#34;Fetch request result&#34; width=&#34;968&#34; height=&#34;73&#34; class=&#34;alignnone size-full wp-image-13018&#34; srcset=&#34;https://www.andrewcbancroft.com/wp-content/uploads/2016/07/printed-result.png 968w, https://www.andrewcbancroft.com/wp-content/uploads/2016/07/printed-result-300x23.png 300w&#34; sizes=&#34;(max-width: 968px) 100vw, 968px&#34; /&gt;&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;Woohoo! I thought this was pretty cool.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Don&amp;rsquo;t forget&lt;/strong&gt;: If you make updates to your model, you need to re-build your app, delete the &amp;ldquo;momd” folder from your playground&amp;rsquo;s resources, re-drag the freshly-compiled &amp;ldquo;momd” file to your playground again to work with the latest version of the model.&lt;/p&gt;
&lt;p&gt;&lt;a name=&#34;potential-usefulness&#34; class=&#34;jump-target&#34;&gt;&lt;/a&gt;&lt;/p&gt;
&lt;h1 id=&#34;potential-usefulness&#34;&gt;Potential usefulness&lt;/h1&gt;
&lt;p&gt;The other important question to ask, besides &amp;ldquo;I wonder if this is possible?” is &amp;ldquo;How is this useful?”&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;Learning. Playgrounds in and of themselves make sense as a learning tool. How cool is it to be able to build the model you&amp;rsquo;re thinking of in the Xcode designer, import that into a playground, and tinker with it as a learning exercise??&lt;/li&gt;
&lt;li&gt;This could also be useful when you need to try out your data model but don&amp;rsquo;t really want to wire it up to an actual user interface yet. Strip away all the UI complexity and just work with the data model… in a playground! It just seems like a more elegant solution to the &amp;ldquo;print it out to the console” method of experimenting with the model.&lt;/li&gt;
&lt;li&gt;There might be situations when you&amp;rsquo;re building semi-complicated &lt;code&gt;NSPredicate&lt;/code&gt; instances for a fetch request – why not get it working in a playground first, then migrate it over to your app? Just an idea!&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;&lt;a name=&#34;related&#34; class=&#34;jump-target&#34;&gt;&lt;/a&gt;&lt;/p&gt;
&lt;div class=&#34;resources&#34;&gt;
  &lt;div class=&#34;resources-header&#34;&gt;
    You might also enjoy&amp;#8230;
  &lt;/div&gt;
  &lt;ul class=&#34;resources-content&#34;&gt;
    &lt;li&gt;
      &lt;i class=&#34;fa fa-angle-right&#34;&gt;&lt;/i&gt; &lt;a href=&#34;https://www.andrewcbancroft.com/2015/02/18/core-data-cheat-sheet-for-swift-ios-developers/&#34; title=&#34;Core Data Cheat Sheet for Swift iOS Developers&#34;&lt;/a&gt;Core Data Cheat Sheet for Swift iOS Developers
    &lt;/li&gt;
    &lt;li&gt;
      &lt;i class=&#34;fa fa-angle-right&#34;&gt;&lt;/i&gt; &lt;a href=&#34;https://www.andrewcbancroft.com/2015/02/25/using-swift-to-seed-a-core-data-database/&#34; title=&#34;Using Swift to Seed a Core Data Database&#34;&lt;/a&gt;Using Swift to Seed a Core Data Database
    &lt;/li&gt;
    &lt;li&gt;
      &lt;i class=&#34;fa fa-angle-right&#34;&gt;&lt;/i&gt; &lt;a href=&#34;https://www.andrewcbancroft.com/2015/03/05/displaying-data-with-nsfetchedresultscontroller-and-swift/&#34; title=&#34;Displaying Data With NSFetchedResultsController and Swift&#34;&lt;/a&gt;Displaying Data With NSFetchedResultsController and Swift
    &lt;/li&gt;
    &lt;li&gt;
      &lt;i class=&#34;fa fa-angle-right&#34;&gt;&lt;/i&gt; &lt;a href=&#34;https://www.andrewcbancroft.com/2015/05/28/sync-table-view-data-nsfetchedresultscontroller-swift/&#34; title=&#34;Sync Table View Data: NSFetchedResultsController and Swift&#34;&lt;/a&gt;Sync Table View Data: NSFetchedResultsController and Swift
    &lt;/li&gt;
  &lt;/ul&gt;
&lt;/div&gt;
&lt;p&gt;&lt;a name=&#34;course&#34; class=&#34;jump-target&#34;&gt;&lt;/a&gt;&lt;/p&gt;
&lt;div class=&#34;resources&#34;&gt;
  &lt;div class=&#34;resources-header&#34;&gt;
    Resources
  &lt;/div&gt;
  &lt;ul class=&#34;resources-content&#34;&gt;
    &lt;li&gt;
      &lt;i class=&#34;fas fa-video&#34;&gt;&lt;/i&gt; &lt;a href=&#34;http://bit.ly/ps-core-data-swift&#34; target=&#34;_blank&#34;&gt;Core Data Fundamentals with Swift&lt;/a&gt;&lt;br /&gt; &lt;a href=&#34;http://bit.ly/ps-core-data-swift&#34; target=&#34;_blank&#34;&gt;&lt;img src=&#34;https://www.andrewcbancroft.com/wp-content/uploads/2017/04/ps-core-data-fundamentals-swift-1024x576.png&#34; alt=&#34;Core Data Fundamentals with Swift&#34; width=&#34;1024&#34; height=&#34;576&#34; class=&#34;alignnone size-large wp-image-13163&#34; srcset=&#34;https://www.andrewcbancroft.com/wp-content/uploads/2017/04/ps-core-data-fundamentals-swift-1024x576.png 1024w, https://www.andrewcbancroft.com/wp-content/uploads/2017/04/ps-core-data-fundamentals-swift-300x169.png 300w, https://www.andrewcbancroft.com/wp-content/uploads/2017/04/ps-core-data-fundamentals-swift-768x432.png 768w, https://www.andrewcbancroft.com/wp-content/uploads/2017/04/ps-core-data-fundamentals-swift.png 1539w&#34; sizes=&#34;(max-width: 1024px) 100vw, 1024px&#34; /&gt;&lt;/a&gt;
    &lt;/li&gt;
  &lt;/ul&gt;
&lt;/div&gt;
&lt;p&gt;&lt;a name=&#34;share&#34; class=&#34;jump-target&#34;&gt;&lt;/a&gt;&lt;/p&gt;
</description>
    </item>
    
    <item>
      <title>Create Documentation for your Swift Playground</title>
      <link>https://www.andrewcbancroft.com/2014/09/09/create-documentation-for-your-swift-playground/</link>
      <pubDate>Wed, 10 Sep 2014 03:44:16 +0000</pubDate>
      
      <guid>https://www.andrewcbancroft.com/2014/09/09/create-documentation-for-your-swift-playground/</guid>
      <description>&lt;p&gt;As I opened Apple&amp;rsquo;s latest &lt;a title=&#34;Apple Swift Developer Blog - Patterns Playground&#34; href=&#34;https://developer.apple.com/swift/blog/?id=13&#34; target=&#34;_blank&#34;&gt;&amp;ldquo;Patterns Playground” blog post&lt;/a&gt;, I was impressed with the look and feel.  Rather than create the documentation as comment blocks, they managed to put in sharp-looking notes and explanations before each code example / experiment.  It&amp;rsquo;s like an interactive book, reminiscent of the Swift Programming Language iBook!  Here&amp;rsquo;s a snippet of what their Playground looks like:&lt;/p&gt;
&lt;p&gt;[&lt;img class=&#34;alignnone size-large wp-image-4771&#34; src=&#34;http://www.andrewcbancroft.com/wp-content/uploads/2014/09/Patterns_playground-1024x511.png&#34; alt=&#34;Apple&#39;s Patterns Playground&#34; width=&#34;730&#34; height=&#34;364&#34; srcset=&#34;https://www.andrewcbancroft.com/wp-content/uploads/2014/09/Patterns_playground-1024x511.png 1024w, https://www.andrewcbancroft.com/wp-content/uploads/2014/09/Patterns_playground-300x149.png 300w, https://www.andrewcbancroft.com/wp-content/uploads/2014/09/Patterns_playground.png 1080w&#34; sizes=&#34;(max-width: 730px) 100vw, 730px&#34; /&gt;][1]&lt;/p&gt;
&lt;p&gt;Of course, my immediate question / thought was, &amp;ldquo;How&amp;rsquo;d they &lt;em&gt;do&lt;/em&gt; that?!  Can &lt;em&gt;I&lt;/em&gt; do that?  I want to do that!”.  I didn&amp;rsquo;t know it at the time of this publication, but apparently Apple has some &lt;a title=&#34;Apple Developer Documentation - Interactive Learning Playgrounds&#34; href=&#34;https://developer.apple.com/library/prerelease/ios/documentation/Swift/Reference/Playground_Ref/Chapters/InteractiveLearning.html&#34; target=&#34;_blank&#34;&gt;pre-release documentation out on developer.apple.com&lt;/a&gt;, detailing how all this is done.  I (as usual) did it the hard way and hacked my way through it.  I figured I&amp;rsquo;d go ahead and share my discoveries alongside the official documentation from Apple.&lt;/p&gt;
&lt;p&gt;I&amp;rsquo;m already a believer in using Playgrounds for teaching.  The read-example-experiment loop is so easy in the Playground environment.  I think using this documentation technique has the potential to enhance the Playground experience &lt;em&gt;even more&lt;/em&gt; to create professional, sharp-looking educational material_.  _&lt;/p&gt;
&lt;p&gt;For this post, I wanted to take a simple example (the File -&amp;gt; New Playground playground) and convert the comment-style documentation into &amp;ldquo;fancy” documentation.&lt;/p&gt;
&lt;h3 id=&#34;before&#34;&gt;Before:&lt;/h3&gt;
&lt;p&gt;[&lt;img class=&#34;alignnone size-large wp-image-4751&#34; src=&#34;http://www.andrewcbancroft.com/wp-content/uploads/2014/09/MyPlayground_WithDocumentation_playground_before-1024x108.png&#34; alt=&#34;Before view of Playground&#34; width=&#34;730&#34; height=&#34;76&#34; srcset=&#34;https://www.andrewcbancroft.com/wp-content/uploads/2014/09/MyPlayground_WithDocumentation_playground_before-1024x108.png 1024w, https://www.andrewcbancroft.com/wp-content/uploads/2014/09/MyPlayground_WithDocumentation_playground_before-300x31.png 300w, https://www.andrewcbancroft.com/wp-content/uploads/2014/09/MyPlayground_WithDocumentation_playground_before.png 1142w&#34; sizes=&#34;(max-width: 730px) 100vw, 730px&#34; /&gt;][2]&lt;/p&gt;
&lt;h3 id=&#34;after&#34;&gt;After:&lt;/h3&gt;
&lt;p&gt;[&lt;img class=&#34;alignnone size-large wp-image-4761&#34; src=&#34;http://www.andrewcbancroft.com/wp-content/uploads/2014/09/MyPlayground_WithDocumentation_playground_after-1024x239.png&#34; alt=&#34;After view of Playground&#34; width=&#34;730&#34; height=&#34;170&#34; srcset=&#34;https://www.andrewcbancroft.com/wp-content/uploads/2014/09/MyPlayground_WithDocumentation_playground_after-1024x239.png 1024w, https://www.andrewcbancroft.com/wp-content/uploads/2014/09/MyPlayground_WithDocumentation_playground_after-300x70.png 300w, https://www.andrewcbancroft.com/wp-content/uploads/2014/09/MyPlayground_WithDocumentation_playground_after.png 1139w&#34; sizes=&#34;(max-width: 730px) 100vw, 730px&#34; /&gt;][3]&lt;/p&gt;
&lt;p&gt;Let&amp;rsquo;s explore.&lt;/p&gt;
&lt;h2 id=&#34;the-gist&#34;&gt;The Gist&lt;/h2&gt;
&lt;ol&gt;
&lt;li&gt;Create yourself a playground (and note where you save it).&lt;/li&gt;
&lt;li&gt;Navigate to where the .playground file is saved in Finder.&lt;/li&gt;
&lt;li&gt;Right-click the .playground file, and choose &amp;ldquo;Show Package Contents”.&lt;/li&gt;
&lt;li&gt;Add a new folder named &amp;ldquo;Documentation”.&lt;/li&gt;
&lt;li&gt;Create a new HTML file in the Documentation folder.  It should contain the HTML markup and the text explanation that you&amp;rsquo;d like to enhance your playground with.  Make sure it is a well-formed HTML document by the time you&amp;rsquo;re finished creating it. I describe a quick way to jumpstart your documentation process at the end of this article…&lt;/li&gt;
&lt;li&gt;Although optional, I&amp;rsquo;d recommend styling your HTML file with appropriate CSS.  Adding a CSS file and referencing it within the HTML file you created is probably a good idea (and this is allowed for Playground documentation).&lt;/li&gt;
&lt;li&gt;Open contents.xcplayground with the text editor of your choice by right-clicking and choosing &amp;ldquo;Open With”.&lt;/li&gt;
&lt;li&gt;Modify the XML so that the &lt;sections&gt; element contains a &lt;span class=&#34;lang:default decode:true  crayon-inline &#34;&gt;&lt;documentation&gt;&lt;/span&gt; node as shown in the highlighted code lines in the example below.  Be sure to replace the relative-path value with the name of the HTML file you created in step 5 (I named mine &amp;ldquo;doc-fragment-0.html”): &lt;pre class=&#34;lang:xhtml mark:2-3 decode:true &#34; title=&#34;contents.xcplayground Snippet&#34;&gt;&lt;sections&gt;
&lt;documentation relative-path=&#39;doc-fragment-0.html&#39;&gt;
&lt;/documentation&gt;
&lt;code source-file-name=&#39;section-2.swift&#39;/&gt;
&lt;/sections&gt;&lt;/li&gt;
&lt;/ol&gt;
&lt;pre tabindex=&#34;0&#34;&gt;&lt;code&gt;
  9.  Save, and open the playground in Xcode 6.  Your Playground should now contain documentation!

As a way to jump-start my own Playground documentation, I simply borrowed one of the HTML files and the CSS file from Apple&amp;#39;s Patterns Playground.  Then I modified the markup to contain the educational material I wanted for _my_ playground, customized the styles to what I wanted, added the &amp;lt;span class=&amp;#34;lang:default decode:true  crayon-inline &amp;#34;&amp;gt;&amp;lt;documentation&amp;gt;&amp;lt;/span&amp;gt; node to the contents.xcplayground file, saved, and voila:  Fancy documentation!

&amp;amp;nbsp;

 [1]: http://www.andrewcbancroft.com/wp-content/uploads/2014/09/Patterns_playground.png
 [2]: http://www.andrewcbancroft.com/wp-content/uploads/2014/09/MyPlayground_WithDocumentation_playground_before.png
 [3]: http://www.andrewcbancroft.com/wp-content/uploads/2014/09/MyPlayground_WithDocumentation_playground_after.png
&lt;/code&gt;&lt;/pre&gt;</description>
    </item>
    
  </channel>
</rss>