Swift Playground - Types Lesson

While I am not new to programming, I am quite new to the Swift language. I am using the Swift Playground app on macOS 26 on an M1 MacBook Air.

I am on the lesson about types. Perhaps it's a silly question, but what is a portal? It is never described or pointed out where to find it in the puzzle world. Similarly, the instructions reference a "switch" object without ever defining what it is. I cannot write code to call methods or set properties on objects about which I have no useful information. Can anyone advise, please?

Thank you kindly.

Answered by DTS Engineer in 858517022

For context, I’m using Swift Playground 4.6.4 on macOS 15.6.1. It has two lessons:

  • Learn to Code 1
  • Learn to Code 2

Your reference to the Types lesson indicates you’re doing Learn to Code 2. Types > Introduction > page 8 introduces the concept of portal, indicating that they’re part of the puzzle world. That concept was introduced in Learn to Code 1 > Commands > Portal Practice. Likewise the concept of switch is in Learn to Code 1 > Commands > Toggling a Switch.

I cannot write code to call methods or set properties on objects about which I have no useful information.

If you’re asking questions like this then I suspect that Learn to Code isn’t really for you. You might be better off starting with The Swift Programming Language, using a general playground, or a playground in Xcode, to play with the many examples it contains.

Share and Enjoy

Quinn “The Eskimo!” @ Developer Technical Support @ Apple
let myEmail = "eskimo" + "1" + "@" + "apple.com"

Accepted Answer

For context, I’m using Swift Playground 4.6.4 on macOS 15.6.1. It has two lessons:

  • Learn to Code 1
  • Learn to Code 2

Your reference to the Types lesson indicates you’re doing Learn to Code 2. Types > Introduction > page 8 introduces the concept of portal, indicating that they’re part of the puzzle world. That concept was introduced in Learn to Code 1 > Commands > Portal Practice. Likewise the concept of switch is in Learn to Code 1 > Commands > Toggling a Switch.

I cannot write code to call methods or set properties on objects about which I have no useful information.

If you’re asking questions like this then I suspect that Learn to Code isn’t really for you. You might be better off starting with The Swift Programming Language, using a general playground, or a playground in Xcode, to play with the many examples it contains.

Share and Enjoy

Quinn “The Eskimo!” @ Developer Technical Support @ Apple
let myEmail = "eskimo" + "1" + "@" + "apple.com"

Thank you so much for your reply. It seems that the term "greenPortal" refers to the circular objects illuminated in green over certain tiles.

I could not solve this exercise and had to look at the solution, which I never would have figured out on my own. The solution introduces functions, which were not discussed previously, as far as I can tell. Also, I am confused (again) by what is a portal and what is a switch.

Perhaps I should abandon using the Swift Playground, as it is, in my opinion, very poorly designed and is not teaching me much about Swift programming. Having already taken several courses in Python, C# and VB.NET, as well as having programmed in Excel VBA for over 27 years, perhaps a less cutesy application that's geared towards experienced programmers would be more suitable for me, as you have indicated. Thank you kindly.

I decided to stick with Swift Playground for the time being. Although I wanted to give up, I decided to take another crack at solving the challenge. Looking at the expected solution provided guidance, but I didn't just copy and paste the code. Instead I experimented with various moveForward() commands to move Byte around. That was perhaps my biggest mental block. I didn't realize that I needed to access each switch, toggle it, and then return to the center tile, rinse() and repeat(). 😍

/*First, we must turn off the portal, otherwise Byte will fall through it and not get to any of those switches
*/
greenPortal.isActive = false

/*Define a function to move Byte forward 3 tiles
*/
func moveThree() {
    for i in 1...3 {
        moveForward()
    }
}

/*Define a function that causes Byte to move back to the center tile, on which is the deactivated green portal thingy
*/
func returnToCenter() {
    //Bring Byte back to center tile
    //from any Toggle it happens to 
    //be on currently
    //Two turns will orient Byte in 
    //the direction opposite to which it
    //moved to the switch
    turnRight()
    turnRight()
    moveThree()
}

/*Write code to do all of the following:
 - Toggle switch on left side of puzzle
 - Toggle switch at far end of puzzle
 - Toggle switch at right side of puzzle
 - After each task is complete, return Byte
   to the center tile, rotate him 90 degrees
   and repeat
 - Finally, with Byte back on the center tile,
 - reactivate the green portal, which will teleport
 - him to the finish line (or tile)
 */

//Get Byte to the center tile
moveThree()

/*The following code applies for all three tiles
*/
for tile in 1...3 {
    turnLeft()
    moveThree()
    toggleSwitch()
    returnToCenter()
}

/*Byte has toggled all three switches
so it's time to finish up
*/
greenPortal.isActive = true

I am glad I stuck with the challenged and worked out how to complete the assigned task. Not sure if my solution is the most streamlined, but it does seem to work.

Thanks again for your kind assistance and guidance.

Swift Playground - Types Lesson
 
 
Q