In order to create a simple test for your decodable code:
import Foundation
// in this case the show name is {"A"-Team}, hence the \\ to espace the {"}
let jsonString = """
{
"id": "345team",
"name": "\\"A\\"-Team",
}
"""
struct Show: Decodable {
let id: String
let name: String
}
guard let rawData = jsonString.data(using: .utf8),
let card = try? JSONDecoder().decode(Show.self, from: rawData) else {
fatalError("couldn't parse the Raw JSON string, the json content must be malformed")
}
print(Show.name)
Useful to provide test data to ViewControllers, when you don't have an internet connection.
Given that you have a sample-data.json
file in your Resources
folder
import Foundation
guard
let jsonFileURL = Bundle.main.url(forResource: "sample-data", withExtension: "json"),
let jsonData = try? Data(contentsOf: jsonFileURL),
let decodedResult = try? JSONDecoder().decode(MyModel.self, from: jsonData) else {
fatalError("coudln't read the file from the main bundle")
}
First, you need to add an image to the Resource
folder of a playground named design.png
, then from the code, you can simply add:
/*:
## Inserting a markdown image
![sample image](design.png)
*/
and it will render correctly
Another one for mac, in playgrounds
import Cocoa
import PlaygroundSupport
class MyViewController: NSViewController {
override func loadView() {
self.view = NSView(
frame: CGRect(x: 0, y: 0, width: 300, height: 200)
)
self.view.wantsLayer = true
self.view.layer?.backgroundColor = NSColor.blue.cgColor
}
}
let viewController = MyViewController()
PlaygroundPage.current.liveView = viewController
The important part, is that the method loadView
must be implemented, as using the empty initializer,
the code will throw an error missing the view if loadView
hasn't been implemented
Given an image added to the Resources folder, like tutorial-01.jpg
, I would open it from a playground like:
let image = UIImage(named: "tutorial-01.jpg")
Given an image added to the Resources folder, like tutorial-01.jpg
, I would open it from a playground like:
let fileURL = Bundle.main.url(forResource: "tutorial-01", withExtension: "jpg")!
let image = NSImage(contentsOf: fileURL)
Tip taken from john sundell's Writing unit tests in Swift playgrounds post
import Foundation
import XCTest
class MyTestClass: XCTestCase {
func testSomething() {
// write test here
}
}
class TestObserver: NSObject, XCTestObservation {
func testCase(_ testCase: XCTestCase,
didFailWithDescription description: String,
inFile filePath: String?,
atLine lineNumber: Int) {
assertionFailure(description, line: UInt(lineNumber))
}
}
let testObserver = TestObserver()
XCTestObservationCenter.shared.addTestObserver(testObserver)
MyTestClass.defaultTestSuite.run()
When attempting to preview an instance of UIView
, it's important to call view.layoutIfNeeded()
otherwise the view would never layout on Playgrounds.
let view = MyCustomView()
view.frame = CGRect(x: 0, y: 0, width: 300, height: 150)
view.layoutIfNeeded()
First, 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:
let fontName = "my-custom-font"
let fontURL = Bundle.main.url(forResource: fontName, withExtension: "otf")
CTFontManagerRegisterFontsForURL(fontURL! as CFURL, .process, nil)
let uiFontName = "MyCustomFont-Bold"
let customFont = UIFont(name: uiFontName, size: 16.0)