Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to fix 'Couldn't lookup symbols' error in Swift Playground

I have an Xcode workspace with a dynamic framework (called 'VisualKit') and a playground, which imports VisualKit.

This setup works fine when VisualKit has no external dependencies of its own (i.e. when VisualKit does not depend on any additional dynamic frameworks).

However, when I add a dynamic framework (in this case I'm importing SnapKit, though it does not matter what framework it is), I get the following error:

error: Couldn't lookup symbols:
  VisualKit.ItemsLabel.__allocating_init() -> VisualKit.ItemsLabel
  type metadata accessor for VisualKit.ItemsLabel

I've tried the following: - Cleaning the project, removing the import statement, and then putting it back and attempting to rebuild the playground - Deleting derived data - Recreating the workspace/playground/framework project from scratch

...all to no avail.

My playground:

import UIKit
import PlaygroundSupport
import VisualKit

class MyViewController: UIViewController {
    override func loadView() {
        let view = UIView()
        view.backgroundColor = UIColor.red

        let label = ItemsLabel()
        label.frame = CGRect(x: 150, y: 200, width: 200, height: 20)
        label.text = "Hello"

        view.addSubview(label)
        self.view = view
    }
}

PlaygroundPage.current.liveView = MyViewController()

ItemsLabel (in VisualKit dynamic framework project):

import UIKit
import SnapKit

public class ItemsLabel: UILabel {
    public init() {
        super.init(frame: .zero)
    }

    required public init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
}

I would expect the playground to work the same with the SnapKit import (in VisualKit) as without.

Any ideas?

like image 754
justinkaufman Avatar asked Jan 20 '19 18:01

justinkaufman


People also ask

How do I run code in playground Xcode?

Simply click the Run button at the bottom-right of the playground, and wait for execution to complete. Xcode now compiles your code, and shows you its result. The default code doesn't output anything to the Console, but you should be able to see the value of the variable str in the sidebar on the right of Xcode.

What is the difference between Swift playground and Xcode?

One of the biggest differences between Swift Playgrounds and Xcode Playgrounds is that Swift Playgrounds are much less powerful and are built more as an educational tool. My biggest fear is that as Apple brings Swift Playgrounds to the Mac that they will stop supporting and growing Xcode Playgrounds.


1 Answers

After many painstaking hours troubleshooting this, I managed to solve it:

  1. In Xcode, select your framework project in the project navigator.

  2. Navigate to the Build Phases tab.

  3. Add a Copy Files phase (by tapping the + button), and select Frameworks from the drop down selector.

  4. Add all carthage frameworks used in your framework to the copy files phase.

Then clean and rebuild your framework and the playground should now work as you expect it to.

Note that this answer only necessarily applies if you’re using Carthage to build 3rd party frameworks used in your framework project.

like image 189
justinkaufman Avatar answered Sep 30 '22 15:09

justinkaufman