{"id":18086305,"url":"https://github.com/fespinoza/howtoplayground","last_synced_at":"2026-01-20T15:02:14.712Z","repository":{"id":143418042,"uuid":"121879947","full_name":"fespinoza/HowToPlayground","owner":"fespinoza","description":"A collection of useful \"how-tos\" for working in Xcode Playgrounds","archived":false,"fork":false,"pushed_at":"2019-07-29T18:14:37.000Z","size":6,"stargazers_count":4,"open_issues_count":0,"forks_count":0,"subscribers_count":0,"default_branch":"master","last_synced_at":"2025-04-06T00:43:39.439Z","etag":null,"topics":["how-to","tips","xcode-playground"],"latest_commit_sha":null,"homepage":null,"language":null,"has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/fespinoza.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2018-02-17T18:14:57.000Z","updated_at":"2024-05-17T19:44:35.000Z","dependencies_parsed_at":null,"dependency_job_id":"c0da73ad-d6bf-46aa-9504-8ec4b78b1d40","html_url":"https://github.com/fespinoza/HowToPlayground","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"purl":"pkg:github/fespinoza/HowToPlayground","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/fespinoza%2FHowToPlayground","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/fespinoza%2FHowToPlayground/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/fespinoza%2FHowToPlayground/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/fespinoza%2FHowToPlayground/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/fespinoza","download_url":"https://codeload.github.com/fespinoza/HowToPlayground/tar.gz/refs/heads/master","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/fespinoza%2FHowToPlayground/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":286080680,"owners_count":28605943,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2026-01-20T14:45:23.139Z","status":"ssl_error","status_checked_at":"2026-01-20T14:44:16.929Z","response_time":117,"last_error":"SSL_connect returned=1 errno=0 peeraddr=140.82.121.5:443 state=error: unexpected eof while reading","robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":false,"can_crawl_api":true,"host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["how-to","tips","xcode-playground"],"created_at":"2024-10-31T16:07:35.569Z","updated_at":"2026-01-20T15:02:14.695Z","avatar_url":"https://github.com/fespinoza.png","language":null,"funding_links":[],"categories":[],"sub_categories":[],"readme":"# How to...in playgrounds\n\n## 9. Decode a JSON string\n\nIn order to create a simple test for your decodable code:\n\n```swift\nimport Foundation\n\n// in this case the show name is {\"A\"-Team}, hence the \\\\ to espace the {\"}\nlet jsonString = \"\"\"\n{\n  \"id\": \"345team\",\n  \"name\": \"\\\\\"A\\\\\"-Team\",\n}\n\"\"\"\n\nstruct Show: Decodable {\n  let id: String\n  let name: String\n}\n\nguard let rawData = jsonString.data(using: .utf8),\n  let card = try? JSONDecoder().decode(Show.self, from: rawData) else {\n    fatalError(\"couldn't parse the Raw JSON string, the json content must be malformed\")\n}\n\nprint(Show.name)\n```\n\n## 8. Decode a JSON file\n\nUseful to provide test data to ViewControllers, when you don't have an internet connection.\n\nGiven that you have a `sample-data.json` file in your `Resources` folder\n\n```swift\nimport Foundation\n\nguard\n  let jsonFileURL = Bundle.main.url(forResource: \"sample-data\", withExtension: \"json\"),\n  let jsonData = try? Data(contentsOf: jsonFileURL),\n  let decodedResult = try? JSONDecoder().decode(MyModel.self, from: jsonData) else {\n    fatalError(\"coudln't read the file from the main bundle\")\n}\n```\n\n## 7. Use images in markdown\n\nFirst, you need to add an image to the `Resource` folder of a playground named `design.png`, then from the code, you can simply add:\n\n```swift\n/*:\n  ## Inserting a markdown image\n  \n  ![sample image](design.png)\n*/\n```\n\nand it will render correctly\n\n## 6. Programatically create an instance of NSViewController\n\nAnother one for mac, in playgrounds\n\n```swift\nimport Cocoa\nimport PlaygroundSupport\n\nclass MyViewController: NSViewController {\n  override func loadView() {\n    self.view = NSView(\n      frame: CGRect(x: 0, y: 0, width: 300, height: 200)\n    )\n    self.view.wantsLayer = true\n    self.view.layer?.backgroundColor = NSColor.blue.cgColor\n  }\n}\n\nlet viewController = MyViewController()\n\nPlaygroundPage.current.liveView = viewController\n```\n\nThe important part, is that the method `loadView` must be implemented, as using the empty initializer,\nthe code will throw an error missing the view if `loadView` hasn't been implemented\n\n## 5. Open a image for iOS\n\nGiven an image added to the _Resources_ folder, like `tutorial-01.jpg`, I would open it from a playground like:\n\n```swift\nlet image = UIImage(named: \"tutorial-01.jpg\")\n```\n\n## 4. Open a image for macOS\n\nGiven an image added to the _Resources_ folder, like `tutorial-01.jpg`, I would open it from a playground like:\n\n```swift\nlet fileURL = Bundle.main.url(forResource: \"tutorial-01\", withExtension: \"jpg\")!\nlet image = NSImage(contentsOf: fileURL)\n```\n\n## 3. Write unit tests\n\nTip taken from john sundell's [Writing unit tests in Swift playgrounds][john-testing-playground] post\n\n```swift\nimport Foundation\nimport XCTest\n\nclass MyTestClass: XCTestCase {\n  func testSomething() {\n    // write test here\n  }\n}\n\nclass TestObserver: NSObject, XCTestObservation {\n  func testCase(_ testCase: XCTestCase,\n                  didFailWithDescription description: String,\n                  inFile filePath: String?,\n                  atLine lineNumber: Int) {\n    assertionFailure(description, line: UInt(lineNumber))\n  }\n}\n\nlet testObserver = TestObserver()\nXCTestObservationCenter.shared.addTestObserver(testObserver)\nMyTestClass.defaultTestSuite.run()\n```\n\n## 2. Test an instance of UIView\n\nWhen attempting to preview an instance of `UIView`, it's important to call `view.layoutIfNeeded()` otherwise the view would never layout on Playgrounds.\n\n```swift\nlet view = MyCustomView()\nview.frame = CGRect(x: 0, y: 0, width: 300, height: 150)\nview.layoutIfNeeded()\n```\n\n## 1. Use a custom font\n\nFirst, add the `my-custom-font.otf` file to the `Resources` folder of the playground, then within the playground, you can use the font as follows:\n\n```swift\nlet fontName = \"my-custom-font\"\nlet fontURL = Bundle.main.url(forResource: fontName, withExtension: \"otf\")\nCTFontManagerRegisterFontsForURL(fontURL! as CFURL, .process, nil)\n\nlet uiFontName = \"MyCustomFont-Bold\"\nlet customFont = UIFont(name: uiFontName, size: 16.0)\n```\n\n[john-testing-playground]: https://www.swiftbysundell.com/posts/writing-unit-tests-in-a-swift-playground\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Ffespinoza%2Fhowtoplayground","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Ffespinoza%2Fhowtoplayground","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Ffespinoza%2Fhowtoplayground/lists"}