8

Note: This is a different question than importing generic swift files (which can be done using the Sources folder).

I have a playground with 2 pages and I would like to use a protocol defined in the first page in the second page. I'll use an example of JSON conversion.

JSON.xcplaygroundpage

import Foundation

protocol JSONConvertible {
    func jsonValue() -> String
}

JSONArray.xcplaygroundpage

import Foundation

//Undeclared type JSONConvertible
extension Array : JSONConvertible {

}

I have tried the following imports:

import MyPlayground
import MyPlayground.JSON
import JSON
import JSON.Contents (in finder the file name is actually Contents.swift)

I have also tried adding JSON.xcplaygroundpage into the Source folder of JSONArray as well as the Resources folder.

Note: I realize that I could put the protocol definition in a separate JSON.swift and include that in my project Sources folder. That doesn't really answer my question.

Jeff
  • 3,829
  • 1
  • 31
  • 49
Kevin
  • 16,696
  • 7
  • 51
  • 68

2 Answers2

8

This is working in Xcode 8.3.3.

For code common to multiple pages, put it in separate files under top-level Sources group. Be sure to have proper Swift access control keywords in the right places.

Note from http://help.apple.com/xcode/mac/8.2/#/devfa5bea3af:

...the auxiliary Swift source file must export it using the public keyword. This includes classes, methods, functions, variables, and protocols.

playground source file Common.swift:

public class DoIExist { public init(){ print("Woot!")} }

Then you can reference it in all of the other pages. Like this:

playground page Page2:

//: [Previous](@previous)

let d = DoIExist()

//: [Next](@next)

You can see that it works because of the console output ("Woot!") and the view results gutter.


To achieve this, I followed the directions at the Apple Xcode documentation about Playgrounds. When Apple inevitably moves those docs and does not provide a forwarding link, read on for how I found it.

I searched for the clues found in the link in cocoapriest's answer: "ios recipes Playground Help Add Auxilliary Code to a Playground". This leads to a document Apple is currently calling "Xcode Help" in a chapter titled "Use playgrounds" in a section titled "Add auxiliary code to a playground".

rustyMagnet
  • 3,479
  • 1
  • 31
  • 41
Jeff
  • 3,829
  • 1
  • 31
  • 49
  • My original intention was to create pages where you walked through creating a protocol and then used it later. However, this mostly answers the question because I could add my own version to subsequent page's Sources folder. – Kevin Jun 15 '17 at 01:48
-1

Just got this to work with this instruction by Apple: https://developer.apple.com/library/ios/recipes/Playground_Help/Chapters/AddAuxilliaryCodetoaPlayground.html

Make sure to declare your classes are public. Also, I had to re-start the Xcode to take the effect.

cocoapriest
  • 1,869
  • 3
  • 22
  • 36
  • To clarify, you were able to declare a file in one page and import into another page without duplicating the file? I wasn't able to accomplish that with the link you provided – Kevin Jun 09 '16 at 15:22
  • are you sure you exactly followed all steps? And: there was no "import" in that sense: it was imported automatically for me. – cocoapriest Jun 09 '16 at 18:46
  • The link is broken. Please fix it and ideally put a description of the steps in the answer so that if Apple moves it again, it doesn't matter so much. The answer as currently written is completely useless. – JeremyP Jan 03 '17 at 11:01
  • Honestly, if you had not posted that link I would have never tracked down where Apple moved the docs. Thank you. – Jeff Jun 14 '17 at 18:26