Swift Playgrounds

RSS for tag

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

Posts under Swift Playgrounds tag

94 Posts
Sort by:

Post

Replies

Boosts

Views

Activity

Xcode Playground "Hello, World!" Error
I've seen a few post about this issue Apple continuously has had for years now. I have tried all the solutions that I have seen, but the error still persists. I have zero to little knowledge on Swift/Xcode, coding in general, and I have no idea where to look for these issues. I believe I am using Xcode 15 on macOS Sonoma V14.0. This the error I keep receiving: error: error while processing module import: error: /var/folders/3v/dmv573gn64b364sgv041tw7c0000gn/T/playground24-a1e85e..swift:3:25: error: expected ',' in #sourceLocation directive #sourceLocation(file: ""Hello, World!".playground", line: 1) I just want to learn but this error is really discouraging lol help!
2
0
766
Oct ’23
Guidance Needed: TextField, Import UI & Other Errors
This is my first app and I'm very new to coding. I started out with enthusiasm until becoming overwhelmed with Swift coding language... The app I'm trying to design is very simple and will go hand in hand with the work I do. I attached a screenshot of the UI of what I'm trying to achieve. All I need the app to do is to let me input the appropriate information, and save it to the table. The problem is that: Swift doesn't import source files like C#, whenever I create a new swift file and try to import the file called User.swift, "no file exists named User" I've looked all over and the examples I'm seeing don't make sense, I dont understand why Xcode/Playground doesn't see it. Instead of using the UI, I'm writing out: TextField(Name, text: ???) I've tried defining a variable in my User.swift file (I can't import which might be the problem). Even if I could import the User.swift file, I'm not even sure of what would go after the "text:____" spot - I've tried using the $name or changing it to "self.name" and there's always a problem I realize this may be trivial to some of you but I appreciate your input. What I'm writing in my User.swift file: import SwiftUI struct User { var name: String var phone: String var company: String var userid: String var clientid: String var errorid: String var notes: String init(name: String, phone: String, company: String, userid: String, clientid: String, errorid: String, notes: String) { self.name = name self.phone = phone self.company = company self.userid = userid self.clientid = clientid self.errorid = errorid self.notes = notes } }
6
0
934
Oct ’23
Prompt User to Choose Location Preference Not Working
At the moment with my app Users have to go to Privacy & Security -> Locations Services -> On -> Scroll down to my app and turn on their Location Preference. I am trying to incorporate in to my app the Code below to make it unecessary to leave the app to enable the Userβ€˜s location to be displayed but it does not work. Please tell me, what am I missing here? import MapKit import SwiftUI import CoreLocation // Prompt User to choose Location Preference // class ViewController: UIViewController, CLLocationManagerDelegate{ var locationManager: CLLocationManager? override func viewDidLoad() { super.viewDidLoad() locationManager = CLLocationManager() locationManager?.delegate = self locationManager?.requestWhenInUseAuthorization() view.backgroundColor = .gray } } // The rest of the Code which is working okay ( Map etc.) // struct Positions: Identifiable { let id = UUID() let name: String let latitude: Double let longitude: Double var coordinate: CLLocationCoordinate2D { CLLocationCoordinate2D(latitude: latitude, longitude: longitude) } } etc. etc.
4
0
785
Oct ’23
Deleting evidence of an app in Settings -> Privacy & Security -> Location Services -> On -> List of apps
Is there a way of deleting an app from the Location Services in the Privacy Setting? Despite deleting the particular app evidence of it remains. Also with a particular app there are several of the same in the list. I do understand there may be one for the Playgrounds version and one for the actual downloaded version but I have more. Is there a way of starting with a clean slate?
0
0
452
Oct ’23
Unable to use SpriteKit with Swift Playground Books
All our custom Playground Books using SpriteKit stopped working after upgrading to SwiftPlaygrounds version 4.4 To reproduce the issue, simply create a new book by clicking the top right hand icon and in the book add two lines of code. import SpriteKit let circle = SKShapeNode(circleOfRadius: 100.0) The second line produces an error. I tried with other classes. SKAction seems to work. SKSpriteNode, SKTexture etc have the same issue. Please help. Our students are stuck!
2
0
737
Oct ’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
590
Nov ’23
Adding functionalities to Swift Playgrounds’ app
Hi there, I am trying to add functionalities for my app developed using Swift Playgrounds environment on mac. There is a plus button that opens an interface where I can choose the feature in question. Until here, no problem you would say! But my problem is that I don’t know which package should I use or even import to develop my application? Best regards,
1
0
379
Nov ’23
Xcode playground projects don't supporting iOS 17?
I wanted to take some code I had written in an Xcode project and put it in a playground, though while working on a playground in Xcode, it's showing errors that some lines of code are only supported in iOS 17. I'm updated to the most recent version. Is there any reason why my playgrounds are showing this error? I assume it might be due to the playground wanting to make sure the app works on older OSs, but a lot of the code I used has no previous counterpart. If this is the case, is there any way I can get around this? Thanks for taking the time to check this out.
1
0
524
Nov ’23
Swift Regex crashes when trying to get a simple string
I'm trying to create a simple Regex Builder to parse a timestamp in hh:mm:ss or mm:ss format, a " - " separator, and then a string. I'm trying to use the new Swift RegexBuilder however it doesn't seem to work. It compiles but crashes when running when trying to get the text portion. Example code in a single playground preview file for reference: import SwiftUI import RegexBuilder struct RegexTestView: View { var string: String { let timecode = Regex { Optionally { OneOrMore(.whitespace) } // timecode section Optionally { Capture { OneOrMore(.digit) } transform: { match in Int(match) } ":" } TryCapture { OneOrMore(.digit) } transform: { match in Int(match) } ":" // seconds TryCapture { OneOrMore(.digit) Optionally { // miliseconds "." OneOrMore(.digit) } } transform: { match in TimeInterval(match) } // separator " - " Capture { // WHY CAN'T I GET THE REMAINING TEXT???? Any attempts I make to get this result in a crash. OneOrMore(.any) } transform: { match in String(match) } } let tests = [" 222:45:23.2 - foo","2:34:22 - bar"," 2:08 - baz","2:32.18","2:45:23.2 - what"] var results = "" for test in tests { guard let match = test.wholeMatch(of: timecode) else { results += "MATCH NOT FOUND" continue } results += """ β€’β€’β€’β€’ \(match.0) \(String(describing: match.1)) \(String(describing: match.2)) \(String(describing: match.3)) """ /* // Adding this line to the above crashes \(String(describing: match.4))\n */ } return results } var body: some View { Text(string) } } #Preview("Regex") { RegexTestView() }
1
0
495
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
407
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
424
Dec ’23