15

I am working on a Swift playground that contains multiple files in the Sources folder alongside the main file. When I type something, for instance, "UI", in the main file, I get the expected autocomplete suggestions. However, when I do the same in files in the Sources folder, I get a "dumb" autocomplete menu as seen in this question: Xcode 9 Autocomplete Not Working 100% - Partially Working

I have tried deleting DerivedData as suggested in the above question, and have even gone to the lengths of reinstalling Xcode entirely. However, the problem has not gone away. I have created a new playground to ensure it is not specific to this project, and the same problem occurs.

Edit: I just tried the same thing on a different Mac and the same problem was observed. I suppose this is simply a bug in Xcode that has nothing to do with this particular installation or project.

brendon-ai
  • 469
  • 1
  • 8
  • 19

3 Answers3

26

In order to enable autocompletion, you can embed your Playground in a regular Xcode project (e.g. an iOS application). I recommend creating a dummy project for that purpose. Simply drag and drop your playground in this dummy project and make sure to check "Add to target".

Then you can navigate to

Target -> Build Phases -> Compile Sources -> + -> Add other

and add all the files from your source folder. Please note, that you don't need to actually copy the files, a reference is enough for this purpose.

After this process all your source files are built against this dummy target and you can use autocompletion as usual. As far as I know, this is the best practice for debugging Playgrounds right now. Anyway I am curious, if there is an easier way to achieve that.

JoRa
  • 1,063
  • 15
  • 28
  • 3
    Note that you can't open the playground package in the "Add Other" dialog in Xcode... open it in Finder and drag the title bar icon into the dialog. https://superuser.com/questions/414410/in-mac-os-x-how-do-i-navigate-into-a-package-from-file-chooser – brendon-ai Mar 18 '18 at 18:40
  • 1
    This is probably the best, but not a nice solution. Apple should fix this. – j3141592653589793238 Mar 24 '18 at 12:40
0

Swift playgroundbooks do not support autocomplete. Try writing the code in a regular Swift playground, and then move the code over to the playgroundbook.

Michael Austin
  • 746
  • 4
  • 20
0

This works for me every time, Restart/Start -> Open any existing Xcode project(use autocomplete) -> Open playground

Autocomplete will work as usual