Swift Playground

RSS for tag

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

Swift Playground Documentation

Posts under Swift Playground subtopic

Post

Replies

Boosts

Views

Activity

Swift Playgrounds
Hi everyone At the moment I try to use augmented reality QuickLook in Swift playgrounds When I try to open the reality file from the resource folder, this error shows up. Have someone an idea to fix that problem? Thanks in advance import QuickLook import RealityKit import ARKit struct ARQLViewController: UIViewControllerRepresentable {     func makeUIViewController(context: Context) -> some UIViewController {         return UINavigationController.init(rootViewController: ViewController())     }     func updateUIViewController(_ uiViewController: UIViewControllerType, context: Context) {} } class ViewController: UIViewController, QLPreviewControllerDataSource {     var isAppeard: Bool = false     override func viewDidLoad() {         super.viewDidLoad()         DispatchQueue.main.asyncAfter(deadline: .now() + 0.1) {             let previewController = QLPreviewController()             previewController.dataSource = self             self.present(previewController, animated: true, completion: nil)         }     }    override func viewDidAppear(_ animated: Bool) {         guard !isAppeard else { return }         isAppeard = true         }     func numberOfPreviewItems(in controller: QLPreviewController) -> Int { return 1 }     func previewController(_ controller: QLPreviewController, previewItemAt index: Int) -> QLPreviewItem {         guard let path = Bundle.main.path(forResource: "SipARAnimation", ofType: "reality") else { fatalError("Couldn't find the supported input file.") }         let url = URL(fileURLWithPath: path)         return url as QLPreviewItem     } }
0
0
960
Feb ’23
Swift Playground version for macOS 12.6
Hello! I want to become an ios developer. But the problem is that my laptop is quite old and is not up to date with version 12.6.3 (Monterey). Version 4.2.1 is currently available in the app store, which is supported by devices of version 13 and higher. Where can I download the version of Swift playground that my laptop supports? This is probably version 4.1.
0
0
1.4k
Feb ’23
How can I force landscape on a swift playgrounds project?
I only have an iPad, no MacOS or iPhone, and I am attempting to create an app for the apple store. I had originally created this app for Android, and I'm trying to recreate it from scratch using the iPad with Swift Playgrounds. It had been going well enough until I ran into this issue. Most threads point to updating Package.Swift, but this doesn't seem to be an option for me. (see https://developer.apple.com/forums/thread/704249) Is there anyway to do it within the Playgrounds tool? Also, I attempted to submit my app to the app store, but it was rejected due to the appearance in portrait mode.... Yes, it won't look right there. But, without the ability to force landscape, I'm going to have to redesign all my views.
1
0
1.2k
Feb ’23
impossible to transport my code from Swift Playgrounds to App Store connect in the preparation of the app on iPad
Hello, I'm having a problem when I want to export my code from SwiftPlaygrounds on my Ipad to the Connect app store and I'm getting the following error message when preparing my app for export The operation failed could end. (PlaygroundDistribution.Distribution-AppRecordCredentialsProviderError error 1.). Credentialed provider request failed with error: "Unexpected nil property at path: 'Actor/ relationships/providerld' but my account is registered as a developer, I don't understand
3
0
625
Feb ’23
Roll right roll left swift playgrounds
hi guy so not sure if I'm asking this question in the right place but trying to learn to code, complete novice, I'm just at the end of the Lear to code 1 in swift playgrounds and I'm stuck on the roll right roll left challange,if if any one could help me out would really appreciate it,while isBlockedLeft || isBlockedRight { moveForward() if isBlocked && isBlockedRight { turnLeft() } if isBlocked && isBlockedLeft { turnRight() } if isBlocked && !isBlockedRight { turnLeft() } while isOnGem { collectGem() } while isOnClosedSwitch { toggleSwitch() } }}
12
0
25k
Feb ’23
Cannot infer contextual base in reference to member 'trailingIcon'
I am a beginner and i am working on the Scrumdinger tutorial . this message keeps popping up "Cannot infer contextual base in reference to member 'trailingIcon'" I do not know how to fix it. Here is my code import SwiftUI struct CardView: View {   let scrum: DailyScrum   var body: some View {     VStack(alignment: .leading){       Text(scrum.title)         .font(.headline)         .accessibilityAddTraits(.isHeader)               Spacer()       HStack {         Label("(scrum.attendees.count)", systemImage: "person.3")           .accessibilityLabel("(scrum.attendees.count) attendees")         Spacer()         Label("(scrum.lengthInMinutes)", systemImage: "clock")           .accessibilityLabel("(scrum.lengthInMinutes) minute meeting")           .labelStyle(.trailingIcon)       }       .font(.caption)     }     .padding()     .foregroundColor(scrum.theme.accentColor)   } } struct CardView_Previews: PreviewProvider {   static var scrum = DailyScrum.sampleData[0]   static var previews: some View {     CardView(scrum: scrum)       .background(scrum.theme.mainColor)       .previewLayout(.fixed(width: 400, height: 60))   } } Any ideas?
5
0
6.4k
Feb ’23
Trying to Print from a Playground within the Swift Playgrounds App on the iPad
I select the 3 dots (top right) --> Create a PDF --> Print --> Print Options -->Print, and nothing happens, the dialog box will close as it is printing, but no print out. I have to Create a PDF --> Save to Files, then Open the Files app and print the PDF just saved from there, not very efficient. Any idea what I might be doing wrong or is it a bug?
0
0
1.3k
Jan ’23
How to write a method to sort out black and red cards Newbie to Swift
import Cocoa import Darwin // STRUCTURES & ENUMS page 19/525 // DATE CHANGES //*********************************************************** // 17Jan2023 We have 2 enumns and one structure here // 1st enum rank //*********************************************************** enum Rank: Int { case Ace = 1 case Two,Three,Four,Five,Six,Seven,Eight,Nine,Ten case Jack,Queen,King func simpleDescription () -> String { switch self { case .Ace: return "ace" case .Jack: return " jack" case .Queen: return " queen" case .King: return " king" default: return String( self.rawValue) } } }// enum close let ace = Rank.Ace let aceRawValue = ace.rawValue let king = Rank.King let kingRawValue = king.rawValue print(kingRawValue) // king raw value should be 13 ok // page 20 4 suits in deck of cards enum Suit { case Spades,Hearts, Diamonds,Clubs func simpleDescription()-> String { switch self { case .Spades: return "spades" case .Hearts: return "hearts" case .Diamonds: return "diamonds" case .Clubs: return "clubs" } } }// enum for suit close let hearts = Suit.Hearts let heartDescription = hearts.simpleDescription() print(heartDescription) // page 21/525 // Use structures to support enums struct Card { var rank: Rank var suit: Suit var black1 = Suit.Spades var black2 = Suit.Clubs var red1 = Suit.Hearts var red2 = Suit.Diamonds func simpleDescription()->String{ return"The rank is \(rank.simpleDescription()) of \(suit.simpleDescription())" } }// struc closingbrace let threeOfSpades = Card(rank: .Three,suit:.Spades) let threeOfSpadesDescription = threeOfSpades.simpleDescription() print(threeOfSpadesDescription) // print the variable // EXPT develop a method to support for color : // red hearts and diamonds. // black spades and clubs
4
0
1.3k
Jan ’23
Playground liveView fails
Hi ! I am trying to run some async code before setting up the playground liveView, but I cannot figure out why it keeps crashing. I started up with a task I was waiting on the result before configuring the liveView, and now I am having all async work in a dedicated function, which seems to work fine : imageView has the correct width, and the line playground preview shows the image I am fetching online ! In both cases, I get the same result. Would that error be obvious to anyone ? import Foundation import UIKit import PlaygroundSupport let image = try await getImage() let imageView = UIImageView(image:image) var liveView: UIView = UIView(frame: CGRect(x: 0, y: 0, width: 1050, height: 1050)) liveView.addSubview(imageView) print("before set liveView. imageView.width = \(imageView.frame.width)") PlaygroundPage.current.setLiveView(liveView) // crash here the getImage prototype being : public func getImage() async throws -> UIImage? And the console output : before set liveView. imageView.width = 960.0 Playground execution failed: error: execution stopped with unexpected state. error: Execution was interrupted, reason: shared-library-event. The process has been left at the point where it was interrupted, use "thread return -x" to return to the state before expression evaluation.
0
0
1.3k
Jan ’23
WWDC22 Playgrounds prototyping Demo already broken
Hi there, Well, here I am again, bouncing off the churn that is Swift. I thought I'd test whether Swift Playgrounds might actually be a decent prototyping tool, went to a Demo titled Build your first Playgrounds app and it is already obsoleted by changes to Swift. So, how do we reconcile ourselves to a coding language that changes to rapidly that code becomes obsolete in a matter of months? Is there going to be a list of API's et al that can be considered to be stable? So, the presenter. uses: "    let teas: OrderedSet = ["Byte's Oolong", "Golden Tippy Assam", "English Breakfast", "Matt P's Tea Party", "Darjeeling", "Genmaicha", "Jasmine Green", "Vanilla Rooibos"]" and fixes the error by using: "import Collections" That import no longer exists, and the OrderedSet error she fixed using that Module flags the same error. Can we help me to understand why this simple process of making a list is so challenging for me, the newbie. Are there plans to stabilize Swift, or support Legacy code in the future? That's bearing in mind that I've written some pretty impressive tools in Tk/tcl, C, Pascal. Thanks. Harley
2
0
1.1k
Jan ’23
Swift Playground - Round up the switches - character won't move
Trying to find an easy solution for the "Round up the switches" level. I think the while-loop is bugged - I have no explanation for the problem... var gemcount = 0 var switchcount = 0 func movement() {     moveForward()     if isBlocked && !isBlockedRight{         turnRight()     }     if isBlocked && !isBlockedLeft{         turnLeft()     } } func collect() {     if isOnGem {         collectGem()         gemcount = gemcount + 1}     } func toggle() {     if isOnClosedSwitch && switchcount != gemcount {         toggleSwitch()         switchcount = switchcount + 1} } while switchcount < gemcount {     movement()     collect()     toggle() }
6
0
1.9k
Jan ’23
Unable to complete Swift Playgrounds exercise "Use Model Data In a View"
I'm trying to proceed through Swift Playgrounds > Keep Going With Apps > Sharing Data Between Values > Use Model Data In a View > but "Next" is grayed out: I've tried following the instructions and opening the ResizableFont file, but it still won't let me proceed. I also tried opening this Playgrounds file on another Mac and the same issue happened.
5
1
995
Jan ’23
Adding a tutorial file results in an "explicitly declare as resources" error
Following the instructions in the Create engaging content for Swift Playgrounds video I am adding a tutorial (Walkthrough.tutorial) file to my app. But, I get an error when I open the app on my iPad because of the non-swift files. I am using Xcode 14.1. The app was created using: File>New>Project>Swift Playgrounds App
0
0
801
Nov ’22
Playgrounds crashes all the time, what do I do?
IPad Playgrounds has started to routinely crash on me. It started crashing while I was trying to work with protocol meta types, so I thought something about that feature was doing it, but I’ve moved onto other projects, or even brand new ones and Playgrounds keeps crashing. It seems to have something to do with comments. If I’m just editing code it’ll keep going and the preview will update, but if I start to edit a comment I’ll see the compiler begin to spin and it crashes. I can post a video later if needed. I’ve rebooted the device and it’s still crashing. What do I do? If I delete and reinstall Playgrounds will all my projects stay there or do I need to back them up somehow?
7
0
4.2k
Nov ’22
Swift Playgrounds
Hi everyone At the moment I try to use augmented reality QuickLook in Swift playgrounds When I try to open the reality file from the resource folder, this error shows up. Have someone an idea to fix that problem? Thanks in advance import QuickLook import RealityKit import ARKit struct ARQLViewController: UIViewControllerRepresentable {     func makeUIViewController(context: Context) -> some UIViewController {         return UINavigationController.init(rootViewController: ViewController())     }     func updateUIViewController(_ uiViewController: UIViewControllerType, context: Context) {} } class ViewController: UIViewController, QLPreviewControllerDataSource {     var isAppeard: Bool = false     override func viewDidLoad() {         super.viewDidLoad()         DispatchQueue.main.asyncAfter(deadline: .now() + 0.1) {             let previewController = QLPreviewController()             previewController.dataSource = self             self.present(previewController, animated: true, completion: nil)         }     }    override func viewDidAppear(_ animated: Bool) {         guard !isAppeard else { return }         isAppeard = true         }     func numberOfPreviewItems(in controller: QLPreviewController) -> Int { return 1 }     func previewController(_ controller: QLPreviewController, previewItemAt index: Int) -> QLPreviewItem {         guard let path = Bundle.main.path(forResource: "SipARAnimation", ofType: "reality") else { fatalError("Couldn't find the supported input file.") }         let url = URL(fileURLWithPath: path)         return url as QLPreviewItem     } }
Replies
0
Boosts
0
Views
960
Activity
Feb ’23
Swift Playground version for macOS 12.6
Hello! I want to become an ios developer. But the problem is that my laptop is quite old and is not up to date with version 12.6.3 (Monterey). Version 4.2.1 is currently available in the app store, which is supported by devices of version 13 and higher. Where can I download the version of Swift playground that my laptop supports? This is probably version 4.1.
Replies
0
Boosts
0
Views
1.4k
Activity
Feb ’23
How can I force landscape on a swift playgrounds project?
I only have an iPad, no MacOS or iPhone, and I am attempting to create an app for the apple store. I had originally created this app for Android, and I'm trying to recreate it from scratch using the iPad with Swift Playgrounds. It had been going well enough until I ran into this issue. Most threads point to updating Package.Swift, but this doesn't seem to be an option for me. (see https://developer.apple.com/forums/thread/704249) Is there anyway to do it within the Playgrounds tool? Also, I attempted to submit my app to the app store, but it was rejected due to the appearance in portrait mode.... Yes, it won't look right there. But, without the ability to force landscape, I'm going to have to redesign all my views.
Replies
1
Boosts
0
Views
1.2k
Activity
Feb ’23
impossible to transport my code from Swift Playgrounds to App Store connect in the preparation of the app on iPad
Hello, I'm having a problem when I want to export my code from SwiftPlaygrounds on my Ipad to the Connect app store and I'm getting the following error message when preparing my app for export The operation failed could end. (PlaygroundDistribution.Distribution-AppRecordCredentialsProviderError error 1.). Credentialed provider request failed with error: "Unexpected nil property at path: 'Actor/ relationships/providerld' but my account is registered as a developer, I don't understand
Replies
3
Boosts
0
Views
625
Activity
Feb ’23
Roll right roll left swift playgrounds
hi guy so not sure if I'm asking this question in the right place but trying to learn to code, complete novice, I'm just at the end of the Lear to code 1 in swift playgrounds and I'm stuck on the roll right roll left challange,if if any one could help me out would really appreciate it,while isBlockedLeft || isBlockedRight { moveForward() if isBlocked &amp;&amp; isBlockedRight { turnLeft() } if isBlocked &amp;&amp; isBlockedLeft { turnRight() } if isBlocked &amp;&amp; !isBlockedRight { turnLeft() } while isOnGem { collectGem() } while isOnClosedSwitch { toggleSwitch() } }}
Replies
12
Boosts
0
Views
25k
Activity
Feb ’23
Cannot infer contextual base in reference to member 'trailingIcon'
I am a beginner and i am working on the Scrumdinger tutorial . this message keeps popping up "Cannot infer contextual base in reference to member 'trailingIcon'" I do not know how to fix it. Here is my code import SwiftUI struct CardView: View {   let scrum: DailyScrum   var body: some View {     VStack(alignment: .leading){       Text(scrum.title)         .font(.headline)         .accessibilityAddTraits(.isHeader)               Spacer()       HStack {         Label("(scrum.attendees.count)", systemImage: "person.3")           .accessibilityLabel("(scrum.attendees.count) attendees")         Spacer()         Label("(scrum.lengthInMinutes)", systemImage: "clock")           .accessibilityLabel("(scrum.lengthInMinutes) minute meeting")           .labelStyle(.trailingIcon)       }       .font(.caption)     }     .padding()     .foregroundColor(scrum.theme.accentColor)   } } struct CardView_Previews: PreviewProvider {   static var scrum = DailyScrum.sampleData[0]   static var previews: some View {     CardView(scrum: scrum)       .background(scrum.theme.mainColor)       .previewLayout(.fixed(width: 400, height: 60))   } } Any ideas?
Replies
5
Boosts
0
Views
6.4k
Activity
Feb ’23
Swift Playgrounds 4.2: No contextual help available while using the App Template
While using the Playground template, I get the contextual Help with the code, if I do Help on a variable it will tell me the Type. Then if I am working on the App template, instead of the Playground, there is not Help available, I will always get the message "No Documentation Available", why?
Replies
1
Boosts
0
Views
1.7k
Activity
Jan ’23
Trying to Print from a Playground within the Swift Playgrounds App on the iPad
I select the 3 dots (top right) --> Create a PDF --> Print --> Print Options -->Print, and nothing happens, the dialog box will close as it is printing, but no print out. I have to Create a PDF --> Save to Files, then Open the Files app and print the PDF just saved from there, not very efficient. Any idea what I might be doing wrong or is it a bug?
Replies
0
Boosts
0
Views
1.3k
Activity
Jan ’23
How to write a method to sort out black and red cards Newbie to Swift
import Cocoa import Darwin // STRUCTURES & ENUMS page 19/525 // DATE CHANGES //*********************************************************** // 17Jan2023 We have 2 enumns and one structure here // 1st enum rank //*********************************************************** enum Rank: Int { case Ace = 1 case Two,Three,Four,Five,Six,Seven,Eight,Nine,Ten case Jack,Queen,King func simpleDescription () -> String { switch self { case .Ace: return "ace" case .Jack: return " jack" case .Queen: return " queen" case .King: return " king" default: return String( self.rawValue) } } }// enum close let ace = Rank.Ace let aceRawValue = ace.rawValue let king = Rank.King let kingRawValue = king.rawValue print(kingRawValue) // king raw value should be 13 ok // page 20 4 suits in deck of cards enum Suit { case Spades,Hearts, Diamonds,Clubs func simpleDescription()-> String { switch self { case .Spades: return "spades" case .Hearts: return "hearts" case .Diamonds: return "diamonds" case .Clubs: return "clubs" } } }// enum for suit close let hearts = Suit.Hearts let heartDescription = hearts.simpleDescription() print(heartDescription) // page 21/525 // Use structures to support enums struct Card { var rank: Rank var suit: Suit var black1 = Suit.Spades var black2 = Suit.Clubs var red1 = Suit.Hearts var red2 = Suit.Diamonds func simpleDescription()->String{ return"The rank is \(rank.simpleDescription()) of \(suit.simpleDescription())" } }// struc closingbrace let threeOfSpades = Card(rank: .Three,suit:.Spades) let threeOfSpadesDescription = threeOfSpades.simpleDescription() print(threeOfSpadesDescription) // print the variable // EXPT develop a method to support for color : // red hearts and diamonds. // black spades and clubs
Replies
4
Boosts
0
Views
1.3k
Activity
Jan ’23
Playground liveView fails
Hi ! I am trying to run some async code before setting up the playground liveView, but I cannot figure out why it keeps crashing. I started up with a task I was waiting on the result before configuring the liveView, and now I am having all async work in a dedicated function, which seems to work fine : imageView has the correct width, and the line playground preview shows the image I am fetching online ! In both cases, I get the same result. Would that error be obvious to anyone ? import Foundation import UIKit import PlaygroundSupport let image = try await getImage() let imageView = UIImageView(image:image) var liveView: UIView = UIView(frame: CGRect(x: 0, y: 0, width: 1050, height: 1050)) liveView.addSubview(imageView) print("before set liveView. imageView.width = \(imageView.frame.width)") PlaygroundPage.current.setLiveView(liveView) // crash here the getImage prototype being : public func getImage() async throws -> UIImage? And the console output : before set liveView. imageView.width = 960.0 Playground execution failed: error: execution stopped with unexpected state. error: Execution was interrupted, reason: shared-library-event. The process has been left at the point where it was interrupted, use "thread return -x" to return to the state before expression evaluation.
Replies
0
Boosts
0
Views
1.3k
Activity
Jan ’23
WWDC22 Playgrounds prototyping Demo already broken
Hi there, Well, here I am again, bouncing off the churn that is Swift. I thought I'd test whether Swift Playgrounds might actually be a decent prototyping tool, went to a Demo titled Build your first Playgrounds app and it is already obsoleted by changes to Swift. So, how do we reconcile ourselves to a coding language that changes to rapidly that code becomes obsolete in a matter of months? Is there going to be a list of API's et al that can be considered to be stable? So, the presenter. uses: "    let teas: OrderedSet = ["Byte's Oolong", "Golden Tippy Assam", "English Breakfast", "Matt P's Tea Party", "Darjeeling", "Genmaicha", "Jasmine Green", "Vanilla Rooibos"]" and fixes the error by using: "import Collections" That import no longer exists, and the OrderedSet error she fixed using that Module flags the same error. Can we help me to understand why this simple process of making a list is so challenging for me, the newbie. Are there plans to stabilize Swift, or support Legacy code in the future? That's bearing in mind that I've written some pretty impressive tools in Tk/tcl, C, Pascal. Thanks. Harley
Replies
2
Boosts
0
Views
1.1k
Activity
Jan ’23
Swift Playground - Round up the switches - character won't move
Trying to find an easy solution for the "Round up the switches" level. I think the while-loop is bugged - I have no explanation for the problem... var gemcount = 0 var switchcount = 0 func movement() {     moveForward()     if isBlocked && !isBlockedRight{         turnRight()     }     if isBlocked && !isBlockedLeft{         turnLeft()     } } func collect() {     if isOnGem {         collectGem()         gemcount = gemcount + 1}     } func toggle() {     if isOnClosedSwitch && switchcount != gemcount {         toggleSwitch()         switchcount = switchcount + 1} } while switchcount < gemcount {     movement()     collect()     toggle() }
Replies
6
Boosts
0
Views
1.9k
Activity
Jan ’23
Unable to complete Swift Playgrounds exercise "Use Model Data In a View"
I'm trying to proceed through Swift Playgrounds > Keep Going With Apps > Sharing Data Between Values > Use Model Data In a View > but "Next" is grayed out: I've tried following the instructions and opening the ResizableFont file, but it still won't let me proceed. I also tried opening this Playgrounds file on another Mac and the same issue happened.
Replies
5
Boosts
1
Views
995
Activity
Jan ’23
Adding a tutorial file results in an "explicitly declare as resources" error
Following the instructions in the Create engaging content for Swift Playgrounds video I am adding a tutorial (Walkthrough.tutorial) file to my app. But, I get an error when I open the app on my iPad because of the non-swift files. I am using Xcode 14.1. The app was created using: File>New>Project>Swift Playgrounds App
Replies
0
Boosts
0
Views
801
Activity
Nov ’22
Playgrounds crashes all the time, what do I do?
IPad Playgrounds has started to routinely crash on me. It started crashing while I was trying to work with protocol meta types, so I thought something about that feature was doing it, but I’ve moved onto other projects, or even brand new ones and Playgrounds keeps crashing. It seems to have something to do with comments. If I’m just editing code it’ll keep going and the preview will update, but if I start to edit a comment I’ll see the compiler begin to spin and it crashes. I can post a video later if needed. I’ve rebooted the device and it’s still crashing. What do I do? If I delete and reinstall Playgrounds will all my projects stay there or do I need to back them up somehow?
Replies
7
Boosts
0
Views
4.2k
Activity
Nov ’22