5

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.

Mindtree
  • 53
  • 1
  • 4
  • I guess nobody cares to respond, so I'm completely stuck! If there's a better place to get this answered, perhaps let me know? – Mindtree Sep 02 '17 at 13:36

2 Answers2

2

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
0x416e746f6e
  • 9,872
  • 5
  • 40
  • 68
  • First, thank you! I've been able to follow this down to item 7 - so far so good. I now get an error "Module complied with Swift 3.1 cannot be imported in Swift 4.0". I realise I'm trying to use libraries complied for Swift 3.1 in my Swift 4 project (which is off topic for this question) so I'll have to tackle that first. But a quick question: I presume that the thing you refer to as 'fake framework' is all the things produced in steps 1-7? – Mindtree Sep 19 '17 at 07:43
  • That error message is due to the build settings of your dependencies. They are configured (.xcodeproj) to use Swift 3.1 while your project is 4.0, and that means incompatibility (until ABI stability is implemented, but that's Swift 5 at the earliest). – 0x416e746f6e Sep 20 '17 at 20:11
  • The "fake framework" is the one that you create in step 1. – 0x416e746f6e Sep 20 '17 at 20:13
  • Thanks a lot, this actually worked, even for an iOS playground. Now is there a way to turn this into playground book for Swift Playgrounds on the iPad? – Gusutafu Mar 12 '18 at 10:25
0

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'
sas
  • 7,017
  • 4
  • 36
  • 50