“Playgrounds provides a testing ground that renders developer code in real time. It has the capability of evaluating and displaying the results of single expressions as they are coded (in line or on a side bar), providing rapid feedback to the programmer. This type of development environment is often referred as REPL (Read–Eval–Print–Loop) and it is useful for learning, experimenting and fast prototyping. Playgrounds was used by Apple to publish Swift tutorials and guided tours where the REPL advantages are noticeable.”
1. Creating a project
Let's name it UsingPlayground
cd into the project
Add podfile by initiating pod inside our project
pod init
Open the podfile
We are going to test the program using Cheers program, so let's add Cheers to pod
pod ‘Cheers’
Save the podfile and close it, now we have to install pods to our project
pod install
Close the project and open the generated UsingPlayground.xcworkspace instead, this should be the file you use everyday to create your app
Click on ViewController and write some code to use Cheers
import UIKit
import Cheers
class ViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
let cheerView = CheerView()
view.addSubview(cheerView)
cheerView.frame = view.bounds
// Configure
cheerView.config.particle = .confetti(allowedShapes: [.rectangle, .circle])
// Start
cheerView.start()
}
}
Build and run the project and see your app in action on the simulator or your phone
2. Intergration with Playground
Now we are ready to add in Playground, for our code to be accessible in Playground, we need to add it into the CocoaTouch framework target.
Select the UsingPlayground workspace, add a new target file called UsingPlaygroundFramework
Now create the Playground
Import Cheers and add in some code to test in Playground
import UIKit
import PlaygroundSupport
import Cheers
class MyViewController : UIViewController {
override func loadView() {
// Use cheer alone
let cheerView = CheerView()
cheerView.frame = CGRect(x: 0, y: 50, width: 200, height: 400)
// Configure
cheerView.config.particle = .confetti(allowedShapes: [.rectangle, .circle])
// Start
cheerView.start()
PlaygroundPage.current.liveView = cheerView
}
}
Run the Playground and you will see the program run on the right side