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

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
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
"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.3k
Nov ’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
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
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
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
627
Oct ’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
865
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
935
Oct ’23
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
767
Oct ’23