Chapter 4 compile error/program crash

Following along in the Chapter 4 tutorial, on page 75 (in the v0.3.pdf version), we are told to add this code: var vertices = [float3(0, 0, 0.5)] to the playground. This statement causes compiler errors in Xcode 9.4.1 and a memory access error when the playground is run in Xcode 10 beta 2.

The Xcode 9 error reported is: [{<<< invalid type >>>}]

The Xcode 9 playground does still run however.

@caroline Can you please help with this when you get a chance? Thank you - much appreciated! :]

The Final version of the playground included also has the same problem. The vertices variable is declared differently in Final: var vertices: [float3] = [[...],[...]]. The gives the error [{<<< invalid type >>>}, {…}].

@brianlawson - unfortunately it’s a bug in Xcode 10 Playgrounds. Playgrounds don’t recognise the simd types such as float2, float3 etc.

I’m surprised that it doesn’t work for you in Xcode 9.4.1. I just tried it here on High Sierra and the final Playground works for me. Did you try the final Playground?

Playgrounds are great for this sort of pedagogical demonstration, but they can be a bit buggy. My hope is that Apple fix it before the final release of Xcode 10.

My suggestion in the meantime is to take the code out of the Playground, and put it into a new Swift file in Sources.

  1. Create a new Swift File in the Sources folder.
  2. Add this code:
import simd
import MetalKit

public func doRender(view: MTKView, device: MTLDevice) {
  
}
  1. cut and paste everything in the Starter Playground from guard let drawable to commandBuffer.commit() into doRender(view:device:)

  2. Call doRender(view: view, device: device) in the Playground.

  3. Add all code into doRender instead of the Playground.

While this should work, I’m giving it a quick test through the chapter now.

Edit: I’m finding that as long as I Cmd S the new Render.swift file (and Shaders.metal when you change it) before I go back and run the Playground, it works fine. It does take a little more concentration to remember where you were placing the code, but that’s actually good for learning how to do it. Just remember to put all the draw calls before renderEncoder.endEncoding()!

You may find it easier to Manually Run the Playground, rather than have it run automatically. Hold down the play (or stop) button at the bottom of the window to switch between the two.

Thanks Caroline, I have already set my Playgrounds to run manually as I’ve found Xcode 9 locks up a lot after code changes that introduce syntax errors too quickly for the editor to keep up. I also stole the Command-Shift-Return to run my Playgrounds so I can run then from the keyboard. :wink:

It’s good(? - how about not surprising) to know the problem is an Xcode bug. I’ve been able to work around various issues by switching back and forth between 9 and 10 as problems crop up. I was able to run the both my version and the final Playgrounds in 9.

I’ve also found that from time to time just copy/pasting everything from a problematic Playground to a new one can clear up weird issues.

I haven’t had to copy / paste to a new Playground, but I do find that I quit and restart a lot. And also be a little patient, because sometimes it takes a bit longer to compile behind the scenes than I expect, and it produces weird errors.

@brianlawson I am happy to report this has been fixed in Xcode 10 beta 4! :sweat_smile:

Sweet! Thanks for letting us know.