Post

Replies

Boosts

Views

Activity

Reply to SwiftUI Sheet race condition
I think this problem appears because Optional unwrapping is not done. (If you check the actual printed value, the value is properly assigned to Optional(1)) How about modifying the code like below? import SwiftUI struct ContentView: View { @State var myVar: Int? @State private var presentSheet: Bool = false var body: some View { VStack { // Uncommenting the following Text() view will "fix" the bug (kind of, see a better workaround below). // Text("The value is \(myVar == nil ? "nil" : "not nil")") Button { myVar = nil } label: { Text("Set value to nil.") } Button { myVar = 1 presentSheet.toggle() } label: { Text("Set value to 1 and open sheet.") } } .sheet(isPresented: $presentSheet, content: { if let myVar = myVar { Text("The value is nil") .onAppear { print(myVar) // prints Optional(1) } } else { Text("The value is not nil") } }) } }
Topic: UI Frameworks SubTopic: SwiftUI Tags:
Feb ’24
Reply to Reality Kit on Swift Playgrounds
Use the code below. (If the Reality Kit File is large, I recommend using the loadModelAsync function.) If your Reality Kit file is a usdz file, try using loadModel instead of loadAnchor. Hope this helps. func makeUIView(context: Context) -> ARView { let arView = ARView(frame: .zero) // Create a cube model let model = try? Entity.loadAnchor(named: "RealityKitFilename") // Create horizontal plane anchor for the content let anchor = AnchorEntity(.plane(.horizontal, classification: .any, minimumBounds: SIMD2<Float>(0.2, 0.2))) if let model = model { anchor.children.append(model) // Add the horizontal plane anchor to the scene arView.scene.anchors.append(anchor) } return arView }
Feb ’24
Reply to View Update Failure
I also experienced a similar problem in the past. How about trying this way? class SaveWords: ObservableObject { static let shared = SaveWords() // others... } import SwiftUI struct SearchView: View { @State private var searchText = "" var save = SaveWords.shared @State var heart: String? @State var disappear = false @State var done = true var body: some View { NavigationView { VStack { if !disappear { SearchBarView(text: $searchText) Spacer() } if searchText != "" { List(.constant(Array(FetchWord.getWordFromStart(start: searchText)).prefix(10).map {Word(word: $0.1)})) { suggestion in NavigationLink(destination: suggestion.IPA.wrappedValue == "error" ? AnyView(EmptyView()) : AnyView(PracticeView(wordSheet: suggestion) .onAppear { disappear = true if done { SaveWords.file = "Favorites" DispatchQueue.main.async { Task { try? await save.load() heart = save.words.contains(suggestion.wrappedValue) ? ".fill" : "" } } } } .onDisappear { disappear = false Task { SaveWords.file = "History" try? await save.load() if save.words.first != suggestion.wrappedValue { save.words.insert(suggestion.wrappedValue, at: 0) try? await save.save() } } } .toolbar { ToolbarItem(placement: .navigationBarTrailing) { Button(action: { SaveWords.file = "Favorites" done = false if heart == "" { heart = ".fill" DispatchQueue.main.async { Task { try? await save.load() if !save.words.contains(suggestion.wrappedValue) { save.words.insert(suggestion.wrappedValue, at: 0) try? await save.save() } } } } else { heart = "" DispatchQueue.main.async { Task { try? await save.load() try? await save.delete(wordToDelete: suggestion.wrappedValue) try? await save.save() } } } done = true }, label: { Image(systemName: "heart" + (heart ?? "")) }) } }) ) { if suggestion.IPA.wrappedValue != "error" { CardView(wordSheet: suggestion) } } } } } Spacer() } } }
Topic: UI Frameworks SubTopic: SwiftUI Tags:
Feb ’24
Reply to Add Close button in FamilyActivityPicker Toolbar
How about using the code below? var body: some View { NavigationStack { FamilyActivityPicker() .toolbar { ToolbarItem(placement: .topBarTrailing) { closeButton } } } } @Environment(\.dismiss) var dismiss private var closeButton: some View { Button { dismiss() } label: { Image(systemName: "xmark") .foregroundStyle(.white) } }
Topic: UI Frameworks SubTopic: SwiftUI Tags:
Mar ’24
Reply to SpatialTapGesture and collision surface's normal?
Yes, simd_float4x4 is correct. I know that AR-related functions defined by Apple, return a simd_float4x4 type that includes not only the x, y, and z axes, but also other information such as angles. So you need to extract the desired information value from the simd_float4x4 array and use it. This answer is not absolute and is just my opinion. When I was developing AR App, I could only find the above method.
Topic: Spatial Computing SubTopic: ARKit Tags:
Mar ’24
Reply to File .zip Example
open your swiftpm file on swift playgrounds and drag&drop your resources(videos, photos etc..). and then compress&submit.
Replies
Boosts
Views
Activity
Apr ’23
Reply to Can I go to the apple event at apple park if I am an apple developer?
If you win the Apple WWDC Swift Student Challenge, you will receive a random invitation.(I hope all the winners go next year...)
Replies
Boosts
Views
Activity
Sep ’23
Reply to Using iPadOS 17 / macOS 14 features in challenge submission
I also think "it should be compatible with all versions from iPadOS 16 / macOS 13.5 onwards". So, I created it by setting the minimum iOS version to 16 in the playground settings.
Replies
Boosts
Views
Activity
Feb ’24
Reply to SwiftUI Sheet race condition
I think this problem appears because Optional unwrapping is not done. (If you check the actual printed value, the value is properly assigned to Optional(1)) How about modifying the code like below? import SwiftUI struct ContentView: View { @State var myVar: Int? @State private var presentSheet: Bool = false var body: some View { VStack { // Uncommenting the following Text() view will "fix" the bug (kind of, see a better workaround below). // Text("The value is \(myVar == nil ? "nil" : "not nil")") Button { myVar = nil } label: { Text("Set value to nil.") } Button { myVar = 1 presentSheet.toggle() } label: { Text("Set value to 1 and open sheet.") } } .sheet(isPresented: $presentSheet, content: { if let myVar = myVar { Text("The value is nil") .onAppear { print(myVar) // prints Optional(1) } } else { Text("The value is not nil") } }) } }
Topic: UI Frameworks SubTopic: SwiftUI Tags:
Replies
Boosts
Views
Activity
Feb ’24
Reply to Swift Student Challenge Vision
I also previously had a problem using the Vision Framework in Playground.(Doesn't work well in preview or simulator) I think it will work if you test it on an actual device.
Replies
Boosts
Views
Activity
Feb ’24
Reply to Submission Requirements for Xcode Playground - can it target iPad only?
Select one of the options depending on which device your app is designed to target. I think I've seen a clause saying that the test will be run in the iPad Playground environment.
Replies
Boosts
Views
Activity
Feb ’24
Reply to Reality Kit on Swift Playgrounds
Use the code below. (If the Reality Kit File is large, I recommend using the loadModelAsync function.) If your Reality Kit file is a usdz file, try using loadModel instead of loadAnchor. Hope this helps. func makeUIView(context: Context) -> ARView { let arView = ARView(frame: .zero) // Create a cube model let model = try? Entity.loadAnchor(named: "RealityKitFilename") // Create horizontal plane anchor for the content let anchor = AnchorEntity(.plane(.horizontal, classification: .any, minimumBounds: SIMD2<Float>(0.2, 0.2))) if let model = model { anchor.children.append(model) // Add the horizontal plane anchor to the scene arView.scene.anchors.append(anchor) } return arView }
Replies
Boosts
Views
Activity
Feb ’24
Reply to View Update Failure
I also experienced a similar problem in the past. How about trying this way? class SaveWords: ObservableObject { static let shared = SaveWords() // others... } import SwiftUI struct SearchView: View { @State private var searchText = "" var save = SaveWords.shared @State var heart: String? @State var disappear = false @State var done = true var body: some View { NavigationView { VStack { if !disappear { SearchBarView(text: $searchText) Spacer() } if searchText != "" { List(.constant(Array(FetchWord.getWordFromStart(start: searchText)).prefix(10).map {Word(word: $0.1)})) { suggestion in NavigationLink(destination: suggestion.IPA.wrappedValue == "error" ? AnyView(EmptyView()) : AnyView(PracticeView(wordSheet: suggestion) .onAppear { disappear = true if done { SaveWords.file = "Favorites" DispatchQueue.main.async { Task { try? await save.load() heart = save.words.contains(suggestion.wrappedValue) ? ".fill" : "" } } } } .onDisappear { disappear = false Task { SaveWords.file = "History" try? await save.load() if save.words.first != suggestion.wrappedValue { save.words.insert(suggestion.wrappedValue, at: 0) try? await save.save() } } } .toolbar { ToolbarItem(placement: .navigationBarTrailing) { Button(action: { SaveWords.file = "Favorites" done = false if heart == "" { heart = ".fill" DispatchQueue.main.async { Task { try? await save.load() if !save.words.contains(suggestion.wrappedValue) { save.words.insert(suggestion.wrappedValue, at: 0) try? await save.save() } } } } else { heart = "" DispatchQueue.main.async { Task { try? await save.load() try? await save.delete(wordToDelete: suggestion.wrappedValue) try? await save.save() } } } done = true }, label: { Image(systemName: "heart" + (heart ?? "")) }) } }) ) { if suggestion.IPA.wrappedValue != "error" { CardView(wordSheet: suggestion) } } } } } Spacer() } } }
Topic: UI Frameworks SubTopic: SwiftUI Tags:
Replies
Boosts
Views
Activity
Feb ’24
Reply to iPadOS Subversion Target
I created a playground assuming version 16.0. For versions after version 16.0, the code was written as follows. if #available(iOS 17.0, *) { // code... } else { // code... }
Replies
Boosts
Views
Activity
Feb ’24
Reply to Reality Composer Project too large for Swift Student Challenge
I also previously created an AR app using Reality Composer in the Swift student challenge. At that time, I also had a problem with file size. I solved the problem by customizing AR elements in Reality Composer. For example, if I were to use a table, I would not use a table object, but instead created it with 5 squares.
Replies
Boosts
Views
Activity
Feb ’24
Reply to SwiftUI destroying view controller when app goes to background
How about using onAppear() or onDisappear()? onAppear onDisappear
Topic: UI Frameworks SubTopic: UIKit Tags:
Replies
Boosts
Views
Activity
Mar ’24
Reply to Add Close button in FamilyActivityPicker Toolbar
How about using the code below? var body: some View { NavigationStack { FamilyActivityPicker() .toolbar { ToolbarItem(placement: .topBarTrailing) { closeButton } } } } @Environment(\.dismiss) var dismiss private var closeButton: some View { Button { dismiss() } label: { Image(systemName: "xmark") .foregroundStyle(.white) } }
Topic: UI Frameworks SubTopic: SwiftUI Tags:
Replies
Boosts
Views
Activity
Mar ’24
Reply to Show keyboard on button action
How about using isFocused modifier? [https://developer.apple.com/documentation/swiftui/environmentvalues/isfocused)
Topic: UI Frameworks SubTopic: SwiftUI Tags:
Replies
Boosts
Views
Activity
Mar ’24
Reply to SpatialTapGesture and collision surface's normal?
Yes, simd_float4x4 is correct. I know that AR-related functions defined by Apple, return a simd_float4x4 type that includes not only the x, y, and z axes, but also other information such as angles. So you need to extract the desired information value from the simd_float4x4 array and use it. This answer is not absolute and is just my opinion. When I was developing AR App, I could only find the above method.
Topic: Spatial Computing SubTopic: ARKit Tags:
Replies
Boosts
Views
Activity
Mar ’24