Using external libraries in Swift 4 playground

2.7k views Asked by At

I've been trying to learn Swift 4, and therefore needed to use Xcode9 Beta as the IDE. I would like to load a large CSV data file to do some data analysis using a playground. The library I'm trying to use is CSVImporter, installed using Carthage, as recommended by the developer.

I've spent a week on this issue, trying to follow guides I've found online, such as here - Stackoverflow, here - Medium, but they all refer to previous versions of Swift and Xcode, and none seem to work.

In general their approach seems to be incorporating the playground into workspace with a project that uses the imported resource. I usually get the "No such module 'CSVImporter'".

My specific question is: How do I set up a Swift 4 Playground so that I can import CSVImporter with Xcode 9 Beta?

While I have this specific task, I think this would be of general interest to the community. I suspect that someone with a far clearer idea than I of how Xcode executes builds and resolves build dependencies would be able to address this fairly easily! Many thanks.

2

There are 2 answers

0
sas On BEST ANSWER

If your external libraries are SPM packages you can create a playground with Arena:

arena https://github.com/finestructure/Gala
  resolving package dependencies
  libraries found: Gala
✅  created project in folder 'SPM-Playground'
4
0x416e746f6e On

Basically, you have it right. Approach is:

  1. Create new XCode project for Cocoa Framework under MacOS
  2. Create Cartfile with your dependencies
  3. Build dependencies (e.g. carthage update --platform macOS)
  4. Import the .framework files that were built (from Carthage/Build/Mac)
  5. Create extra build phases step that will run script /usr/local/bin/carthage copy-frameworks and copy imported frameworks (just follow Carthage's guidelines, it's all the same so far)
  6. Create some .swift file (e.g. main.swift) that will just import frameworks that you have imported via Carthage
  7. Save XCode project as a workspace file
  8. Create new playground (also MacOS) and save it under the projects root folder of your "fake" framework
  9. Add the playground into workspace (as of XCode 9 it's not possible to create playgrounds directly in the projects/workspaces, so you need to do this trick with adding an existing file to the workspace)
  10. Build your project
  11. In the playground file, import your "fake" framework (see step 1), and only after import your carthage dependency framework.
  12. Profit