Search code examples
iosswiftswift-playgroundxcplayground

Dependent frameworks with playgrounds


Let's say I have 2 frameworks:

  • MyApp.framework
  • FancyLabels.framework

MyApp.framework has a storyboard that uses classes from FancyLabels.framework.

If I create an app target that links and embeds both frameworks, I can instantiate view controllers from MyApp.framework no problem.

But if I try and instantiate a view controller from MyApp.framework in a playground, it can't find the classes from FancyLabels.framework.

My playground code:

//: Playground - noun: a place where people can play

import UIKit
import MyApp
import FancyLabels

let bundle = Bundle(for: MyAppViewController.self)
let storyboard = UIStoryboard(name: "Main", bundle: bundle)
let vc = storyboard.instantiateViewController(withIdentifier: "MyAppViewController")
let view = vc.view

The output in the console:

2017-01-22 19:18:17.016 MyPlayground[98260:3124481] Unknown class _TtC18FancyLabels11StyledLabel in Interface Builder file.
2017-01-22 19:18:17.037 MyPlayground[98260:3124481] Failed to set (styleString) user defined inspected property on (UILabel): [<UILabel 0x7fcf175017c0> setValue:forUndefinedKey:]: this class is not key value coding-compliant for the key styleString.

Solution

  • Finally figured this out. The issue was that my "FancyLabels" framework was using a prebuilt framework via Carthage (XCGLogger). The solution was to follow the appropriate steps to use a Carthage framework in a playground, namely to add the framework's project from Carthage/Checkouts to my workspace and build it. Then everything works as expected, even the playground that is inside the framework project, with full usage of FancyLabels in the MyApp framework's storyboards/xibs.

    For people coming to this question that are strugging with setting this up even without Carthage, I've done a quick working example of how to set this up here: https://github.com/mfclarke/NestedFrameworksInPlaygrounds/