Swift Playgrounds

RSS for tag

Learn and explore coding in Swift through interactive learning experiences on the Swift Playgrounds app for iPadOS and macOS.

Swift Playgrounds Documentation

Post

Replies

Boosts

Views

Activity

Get Started with Apps Stuck in Playgrounds
The “Get Started with Apps” in Playgrounds looks great but it won’t allow me to advance to the next activity. It seems to think that my solution is incorrect, but what I did matches the solution provided in the “hint.” This could be corrected by either allowing for more variety in the responses, or by allowing the user to advance to the next activity regardless of whether the response they provided was correct.
10
2
2.5k
Dec ’21
"public headers ("include") directory path for 'Guide' is invalid or not contained in the target" error
Hi, I have a problem with making a swift playgrounds app walkthrough. I followed the steps exactly and modified my Package.swift a bit (in the targets section): .executableTarget(name: "App", dependencies: ["Guide"], path: "App"),         .target(             name: "Guide",             path: "Guide",             resources: [             .process("Guide.tutorial"),             ]) Now when I open the project in Playgrounds I get this error: public headers ("include") directory path for 'Guide' is invalid or not contained in the target Any help would be appreciated. Thanks
2
0
2.2k
Sep ’22
I-Visualization Revisited Exercise - Develop in Swift Explorations | Xcode 11
I'm encountering the same problem, the chart view stays blank! The code supposed to draw a PiChart with a customization in its style. The exercise is as follows: Pie Charts, Revisited The new API for pie charts exposes two new types: PieWedge and PieChartView. The PieWedge struct gives you several ways to create visual effects with pie charts. It has the following properties: proportion: The percentage of the pie occupied by the wedge, expressed as a Double. color: The color of the wedge. You can use any one of the following values. (Remember to use a period before the color name. If you don't, Swift will return a "use of unresolved identifier" error.) .black .blue .brown .cyan .darkGray .gray .green .lightGray .magenta .orange .purple .red .yellow scale: The radius of the wedge relative to the pie's natural radius, expressed as a Double. Less than 1.0 will make the wedge smaller than normal-sized wedges, and greater than 1.0 will make the wedge larger (typically the desired effect). offset: The distance a wedge lies from the center of the pie, relative to the size of the wedge. An offset of 0 keeps the wedge at the center of the pie. An offset of 1.0 moves the center point of the wedge to where its outer edge would be. The makePieChart() function creates an instance of a PieChartView named pieChartView. PieChartView has one property named wedges, which is an array of PieWedge instances. Assign an array of wedges to this property, or use the append() method of Array to add them one at a time. Keys makePieChart() also creates a key named keyView. It's an instance of ChartKeyView, which has a keyItems property. keyItems is an array of ChartKeyItem instances. ChartKeyItem has the following properties: color: The color swatch displayed in the key. You can use any of the following values. .black .blue .brown .cyan .darkGray .gray .green .lightGray .magenta .orange .purple .red .yellow name: The text to display expressed as a String. Create a pie chart using your own data. Play with different scale and offset properties to see how they affect the look of your chart. My Code: makePieChart() let myPiWedge1 = PieWedge(proportion: 0.1, color: .red, scale: 1.0, offset: 1.0) let myPiWedge2 = PieWedge(proportion: 0.4, color: .blue, scale: 1.0, offset: 1.0) let myPiWedge3 = PieWedge(proportion: 0.5, color: .yellow, scale: 1.0, offset: 1.0) var myPieChartViewArray = PieChartView().wedges myPieChartViewArray = [myPiWedge1, myPiWedge2, myPiWedge3] var myKeyView = keyView.keyItems let myChartKeyItem1 = ChartKeyItem(color: .red, name: "w1") let myChartKeyItem2 = ChartKeyItem(color: .blue, name: "w2") let myChartKeyItem3 = ChartKeyItem(color: .yellow, name: "w3") myKeyView = [myChartKeyItem1, myChartKeyItem2, myChartKeyItem3] I tried this as well, it shows the same issue: makePieChart () //Create an array of PieWedge instances var wedges: [PieWedge] = [] // Create individual PieWedge instances and add them to the wedges array let wedge1 = PieWedge(proportion: 0.3, color: .red, scale: 1.0, offset: 0) let wedge2 = PieWedge(proportion: 0.2, color: .blue, scale: 0.8, offset: 0.2) let wedge3 = PieWedge(proportion: 0.5, color: .green, scale: 1.2, offset: 0) wedges.append(wedge1) wedges.append(wedge2) wedges.append(wedge3) // Create an instance of PieChartView and assign the wedges array let pieChartView = PieChartView() pieChartView.wedges = wedges // Create an instance of ChartKeyView and configure its keyItems array let keyItem1 = ChartKeyItem(color: .red, name: "Red Wedge") let keyItem2 = ChartKeyItem(color: .blue, name: "Blue Wedge") let keyItem3 = ChartKeyItem(color: .green, name: "Green Wedge") let keyItems = [keyItem1, keyItem2, keyItem3] let keyView = ChartKeyView() keyView.keyItems = keyItems Any one can help? to me it sounds that the playground has some bug in it :) This exercise from the book "Explorations": Exploration --> Student --> 3-Organizing Data --> 2-Play with Complex Data --> I-Visualization Revisited
1
0
526
Jul ’23
Use WeatherKit in an Xcode playground?
Can it be done? I can reliably crash Xcode by attempting the following in a playground: import UIKit import WeatherKit import CoreLocation let sanFrancisco = CLLocation(latitude: 37.7749, longitude: 122.4194) let weatherService = WeatherService() let weather = try await weatherService.weather(for: sanFrancisco) let temperature = weather.currentWeather.temperature print(temperature) Xcode 14.3.1 macOS 13.4.1
8
1
1.1k
Jul ’23
Not able to reference classes/structs in Swift playground sources
I have to admit that this is strange for me. Though I have been using playgrounds for years, but I only write small pieces of code to test simple ideas, and I never used another source file. Today I want to add a new struct in Sources folder. To my surprise, I am not able to reference the struct in the main playground file. Sources/testlets.swift: struct Dummy { var name: String } MyPlayground: var box = Dummy(name: "abc") // error: /.../MyPlayground.playground:22:11 Cannot find 'Dummy' in scope
2
0
598
Aug ’23
Any approaches to Swift Playgrounds on macOS Monterey?
Hi, dear community, I recently updated my old Macbook pro(model 2016) to Monterey(which is the latest macOS that support 2016 model), but the Swift Playgrounds on AppStore requires macOS 13. Is there any way I can get the app, just like Xcode? Xcode provides an alternative way to download the xip file of an older version, but playgrounds does. Many thanks in advance, any help will be appreciated!
1
0
610
Sep ’23
XCode 15 bug - Playground cannot find file inside Resources
I have the simplest Playground to display image. I have images inside Resources. Somewhat it does not work and I am really confused. I am using macOS XCode. I think on iPad Swift Playground this might work. import SwiftUI import PlaygroundSupport struct ContentView: View { var body: some View { Image("image1") .resizable() .frame(width: 512, height: 512) } } let hostingController = UIHostingController(rootView: ContentView()) PlaygroundPage.current.liveView = hostingController
2
1
685
Sep ’23
Swift Playgrounds beta with support for iOS 17 and macOS 14 expired!
Since Swift Playgrounds was able to submit Apps to the AppStore ,Apple stated that it can be used to develop apps without having to use Xcode. But if you do so you are lost: The Testflight beta versions of Swift Playgrounds with API support for iOS 17 and macOS 14 have expired one week before the new systems become/became available and there is still no final version! If you used Playgrounds to develop an iOS 17 ready version of your app you can't do anything now and that at a time where the users get their hands on the new OS. That is really bad.
3
3
838
Sep ’23
Playground issue
Hi, I'm just starting out and I'm trying to learn Swift with various tools including the Playground and I'm doing the "continue with the apps" exercises. I'm stuck at the point where I need to add the Colorpicker to change the color. The code seems right but the app won't let me move forward. struct CreatureDetail: View { /#-code-walkthrough(creatureDetail.intro)/ /#-code-walkthrough(creatureDetail.creatureConstant)/ let creature : Creature /#-code-walkthrough(creatureDetail.creatureConstant)/ //#-learning-code-snippet(addStateVarIsScaled) /*#-code-walkthrough(creatureDetail.stateVars)*/ @State var color = Color.white @State var shadowRadius : CGFloat = 0.5 @State var angle = Angle(degrees: 0) /*#-code-walkthrough(creatureDetail.stateVars)*/ var body: some View { VStack { Text(creature.emoji) .resizableFont() .colorMultiply(color) .shadow(color: color, radius: shadowRadius * 40) .rotation3DEffect(angle, axis: (x: 0.0, y: 1.0, z: 0.0)) ColorPicker("Choose a Color", selection: $color) .padding(.horizontal) } } }
2
0
574
Nov ’23
Not able to run Playground Basics from Swift Explorations
Hi, I wanted to learn basics on developing in iOS and started with Swift Explorations. I have installed Xcode 15.0.1 and running it on Ventura 13.5.1. When I execute the Playgroung Basics.playground file, I get an error message saying "Failed to attach to stub for playground execution". Any clue as to why that might be? I am able to create my own playground and execute it properly. I searched around but could not find anything on the topic (other than disablig Rosetta which is not an option as far I can tell in my case; don't see that checkbox under Get Info). Any pointer would be welcome. Cheers, Piloo
2
0
389
Dec ’23
Show Table in SwiftUI Playground
I am trying to generate a simple playground to display Table structure with several TableColumns. Basically, generated a new blank playground; add import statements for SwiftUI and PlayGround support; add structure for content view and a statement for invoking that view as: `PlaygroundPage.current.setLiveView(ContentView()) In general, all of the view components work as expected EXCEPT for the table structure which does not display anything. Its basic structure is: Table(rows, selection: $selRow) { TableColumn("ID") {Text(String($0.id))} TableColumn("Name", value: \.nt) } where "rows" is an array of the structure TRow: struct TRow : Identifiable { var id:Int var num:Int var nt:String } Is there some special trick to allowing a SwiftUI Table to be displayed in Playground?
1
0
401
Dec ’23