Notes on adding external .framework files to playground.
Framework + Playground demo:
Framework + Playground demo from GitSync on Vimeo.
Using .framework in playground:
In order to use multiple .framework’s in playground you have to:
-
Create a new xcode project
-
Save the xcode project as a workspace
-
Add a playground to the workspace (select the project and cmd + n)
-
Name it MyPlayground and set the group to the workspace. 🔑
-
File -> new target -> Cocoa framework -> name the framework (FrameWork.framework)
-
Add the .framework files to the folder of your xcode project
-
Drag the .framework files into the project (set target membership to FrameWork.framework)
-
Cmd + b (aka build)
-
Add this to your playground:
@testable import FrameWork //<--links your external .framework files
@testable import ThirdPartyFrameWork
ThirdPartyFrameWork.test()//Output: hello world
Using nested .framework’s in playground:
Usually .frameworks rely on other .framework’s to work. (Playground doesn’t allow this out of the box)
-
File -> new project -> cocoa framework -> call it Child.framework -> cmd+b
-
File -> new project -> cocoa framework -> call it Parent.framework
-
Copy the Child.framework into the project folder of: Parent.framework
-
Drag the child.framework into XCode where you have the Parent XCode project
-
In the Parent.framework project: cmd + b (aka build)
-
File -> new project -> Call it: “MyApp”
-
Save the xcode project as a workspace
-
Cmd + n -> playground -> name it MyPlayground
-
Name it MyPlayground and set the group to the MyWorkspace. 🔑
-
File -> new target -> cocoa framework -> Call it MyFrameWork.framework
-
Copy Child.framework and Parent.framework into the MyApp project folder
-
Drag Child.framework and Parent.framework into the MyApp xcode project
-
Add Child.framework and Parent.framework in: general -> “embedded binaries”
-
Cmd + b (aka build)
-
When you run an app you can import the framework with
@testable import Parent
and it will work -
In playground you have to make sure you add: (the order of imports is important) 🔑
@testable import MyFrameWork//<--links your external .framework files
@testable import Child
@testable import Parent
Parent.myMethod()//hello parent, hello child
Final notes:
- Having the ability add frameworks to XCode is key to low build times and is also the only way to add external code to playground.
- Being able to nest frameworks is a must when using Third Party FrameWorks and your own frameworks.
- The workflow to get nested frameworks to work in playground is not optimal. An alternative is to compile all libraries as a single .framework. This can be done by creating empty .framework files so that the import statements doesn’t complain. They need to be empty so that the compiler doesn’t complain about duplicate classes. The final step is to copy the third-party framework .swift files into both your project folder and xcode 🔑. Then follow the steps in “Using framework in playground”
Gist for the above example