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

Posts under Swift Playgrounds tag

279 Posts
Sort by:
Post not yet marked as solved
0 Replies
582 Views
The following code does not work in an Xcode Playgrounds or the Swift Playgrounds App on iOS oder macOS. But the same code works when used in an app. Does anyone know how to get the notifications running in a Playground? import GameController import Combine import SwiftUI import PlaygroundSupport PlaygroundPage.current.needsIndefiniteExecution = true var cancellables = Set<AnyCancellable>() NotificationCenter.default.publisher(for: NSNotification.Name.GCControllerDidConnect) .subscribe(on:DispatchQueue.global())     .print()     .receive(on: DispatchQueue.main)     .sink { notification in         print(notification)     }.store(in: &cancellables) let disConnect = NotificationCenter.default.publisher(for: NSNotification.Name.GCControllerDidDisconnect)     .print()     .subscribe(on:DispatchQueue.global())     .receive(on: DispatchQueue.main)     .sink { value in         print(value)     }.store(in: &cancellables)
Posted
by
Post not yet marked as solved
1 Replies
444 Views
Hi! I'm fairly new to Swift and was trying to mess around with it in Xcode and Playgrounds. The thing is that it refuses to print anything to the console. I have it on "automatic run" (signified by the purple play button) but have also tried it in "manual run". Does anyone have an idea about what is going on?
Posted
by
Post not yet marked as solved
0 Replies
248 Views
Is there a way to make this code correctly function, loading a webpage (works using iPad playgrounds, not MacOS playground)? import SafariServices import Foundation import PlaygroundSupport import UIKit import WebKit scene.clear() scene.backgroundImage = imageLiteral(resourceName: "add own foto") let points = scene.circlePoints(radius: 200, count: 100) var pointIndex = 0 func doSomething(touch: Touch) {   if touch.previousPlaceDistance < 60 { return }   let EmojiIndex = "🤱👨‍👩‍👦👨‍👩‍👧‍👦🚆🚙🚴‍♂️🏊‍♂️🏌️‍♂️📱🧸🌳🍏👨🏻‍🦳👵🏻".componentsByCharacter()   let Karakters = Graphic(text: EmojiIndex.randomItem)   scene.place(Karakters, at: touch.position)   Karakters.scale = 0.5   if pointIndex >= points.count {     pointIndex = 0   }   pointIndex += 1   let position = points[pointIndex % points.count]   Karakters.move(to: position, duration: 1.0) } let tool1 = Tool(name: "Tool 1", emojiIcon: "1️⃣") tool1.onTouchMoved = doSomething(touch:) scene.tools.append(tool1) func buttonPressed () {      let test = SFSafariViewController(url: URL(string:"enter_url")!)   test.title = "Open webpage"   PlaygroundPage.current.liveView = UINavigationController(rootViewController: test)   } } let button = Button(name: "Open website") button.onPress = buttonPressed scene.button = button Kind regards, David
Posted
by
Post marked as solved
2 Replies
500 Views
I'm using playground to experiment with Combine. I found this example on a blog. I expect it to create the Future publisher, wait a couple seconds then send the promise and complete. import Foundation import Combine import SwiftUI let future = Future<Int, Never> { promise in     print("Creating")     DispatchQueue.global().asyncAfter(deadline: .now() + 2) {         print("sending promise")         promise(.success(1))     } } future     .sink(receiveCompletion: { print("receiveCompletion:\($0)") },             receiveValue: { print("receiveValue:\($0)") }) print("end") The output I expect: Creating end sending promise receiveValue: ... receiveCompletion: ... The output I get: Creating end sending promise I don't see an indication the promise was executed. What am I doing wrong?
Posted
by
Post not yet marked as solved
0 Replies
326 Views
I am trying to use the playground to test some simple code. However, I cannot get it to work. Even the most simple hello world does not work. The build status at the top says build succeeded but there is no output to either the console or the live view. Here is the code I have been testing with: import UIKit var greeting = "Hello, playground" print(greeting)
Posted
by
Post not yet marked as solved
1 Replies
340 Views
Hi, The code below from the demo works on the MAC but not on the iPad, it Aborts on the Circle() line, if I remove the newly added Text command the code run's again. What am I doing wrong? import SwiftUI import PlaygroundSupport struct ProgressView: View {       var body: some View {     ZStack {         Circle()           .stroke(lineWidth: 40)           .foregroundColor(.blue)       Text("25%")     }   } } PlaygroundPage.current.setLiveView(ProgressView()   .padding(150))
Posted
by
Post not yet marked as solved
1 Replies
912 Views
Hello 👋, I recently started running the new macOS beta [macOS Monterey] everything, all my apps, work great however I did notice since I updated that the functionality to see my Results Bar and Debug area on Xcode have disappeared. To confirm this I created a new blank playground on Xcode, and I tried resseting my workspace. However, no code is showing on the results bar, very strange. Could someone help me out figure out what is going? Could this be an issue on my part or there are more users with a similar problem? Thanks
Posted
by
Post not yet marked as solved
2 Replies
341 Views
hello, it's my first day with swift and my first-day programming in general and I'm already facing a problem... I don't understand why when I try to execute the "script" by clicking on the arrow on line 1 or on the " play" button at the bottom, the script is not shown to me either in the right column or at the bottom... Could someone kindly help me
Posted
by
Post not yet marked as solved
0 Replies
277 Views
Hello! I am fairly new to Swift, and I am trying out random things inside of Swift. I also tried learning with Swift Playgrounds, but it feels like code.org only that you type in functions. If you know the best learning methods, please let me know! Thanks, WE P.S. I know Roblox LuaU, so Swift is not my first language.
Posted
by
Post marked as solved
2 Replies
403 Views
I want to make a truth or dare game in swift playground for iPad but I can't make a button that prints more than one dare or truth. I tried to make a var that have all dares and then i put it in the (class Responser NSObject )the var looks like this( var dares = ["dare1", "dare 2" ...]) but it is giving me error that says (cannot assign value of type '[String]'to type 'String') and the 2nd thing I tried was (let dares = ["dare 1", "dare 2"…]) that gave me the same error. my code looks like this import PlaygroundSupport import UIKit let View = UIView() View.backgroundColor = .blue let lbl = UILabel(frame: CGRect(x: 210, y: 329, width: 200, height: 50)) lbl.text = "hi!" View.addSubview(lbl) lbl.textColor = .white let button = UIButton(frame: CGRect(x: 67, y: 500, width: 100, height: 50)) button.backgroundColor = .green //button.setTitleColor(#colorLiteral(red: 0.0, green: 0.0, blue: 0.0, alpha: 1.0), for: UIControlState.selected) button.setTitle("button", for: UIControl.State.selected) button.layer.cornerRadius = 10 View.addSubview(button) let button1 = UIButton(frame: CGRect(x: 300, y: 500, width: 100, height: 50)) button1.backgroundColor = .red //button1.setTitleColor(#colorLiteral(red: 0.0, green: 0.0, blue: 0.0, alpha: 1.0), for: UIControlState.selected) button1.setTitle("button1", for: UIControl.State.selected) button1.layer.cornerRadius = 10 View.addSubview(button1) class Responser: NSObject {          //Method to be called     @objc func printname()     {         lbl.text = "1"     } } let responder = Responser() button.addTarget(responder, action: #selector(Responser.printname), for:.touchUpInside)   class Responser1: NSObject {          //Method to be called     @objc func printname()     {         lbl.text = "2"     } } let responder1 = Responser1() button1.addTarget(responder1, action: #selector(Responser1.printname), for:.touchUpInside) PlaygroundPage.current.liveView = View
Posted
by
Post not yet marked as solved
1 Replies
434 Views
I Have installed swift playground on my ipad.. but i saw Many different between the show for swift playground on the event wwdc21.. Here: https://youtu.be/3ELYyOwJCpM |.. Where this radical difference is clear..and my second question is if there is a difference between swift playground and xcode playground?
Posted
by
Post not yet marked as solved
2 Replies
1.1k Views
Hi, you say swift playground 5 will come to ipad when the ipados 15 come out, yesterday the Ipados 15 came out i update my ipad but swift playground 5 update didn’t came out with Ipados 15. so i want to know when the swift playground 5 came out and how can i update it
Posted
by
Post marked as solved
1 Replies
462 Views
I have updated to iPadOS 15 but I still have the old interface. However I would like the new interface announced at WWDC21. Is this possible yet?
Posted
by