I am currently working on a project for the Swift Student Challenge. One part of the app is for visualizing goals with a chart created using Swift Charts. In the app, you log your progress for the goal and then it should show up in the chart. My issue is that the data is not showing after it has been logged. Here are some code snippets:
Code For Chart View
Chart {
let currentDate = Date.now
BarMark (
x: .value("Day", "Today"),
y: .value("Score", goalItem.getLogItemByDate(date: currentDate).score)
)
}
.frame(maxHeight: 225)
.padding()
GoalItem Data Object
public class GoalItem: Identifiable {
public var id: String
var name: String
var description: String
var logItems: [String: GoalLogItem]
init(name: String, description: String) {
self.id = UUID().uuidString
self.name = name
self.description = description
self.logItems = [:]
}
func log(date: Date, score: Double, notes: String) {
self.logItems[dateToDateString(date: date)] = GoalLogItem(date: date, score: score, notes: notes)
}
func getLogItemByDate(date: Date) -> GoalLogItem {
let logItem = self.logItems[dateToDateString(date: date)]
if logItem != nil {
return logItem!
} else {
return GoalLogItem(isPlaceholder: true)
}
}
}
After logging something using the GoalItem.log method, why does it not show up in the chart? Are the variables not updated? If so, how would I get the variables to update?
Thanks
Hi? I want to participate Swift Student Challenge. And I am wondering is it okay to make an app which can access album?
I read Terms and Conditions and found no relevant content. Can anyone tell me whether it is possible? Thank you.
Post not yet marked as solved
Hi,
On the Swift Student Challenge submission page the dropdown for “Which software should we use to run your app?” lists “Xcode 15 on macOS 13.5 or later” as one of the options.
Is it ok to submit an Xcode Playground that targets iPadOS (and not macOS)? And then this would just be tested in the iPad Simulator?
Thanks!
Hello,
Does anyone know how to restrict my app playground to run on iPhones only (not even iPads) and to always be presented in portrait mode? I'm using Xcode.
Thanks for any help!
Post not yet marked as solved
Hello,
The appearance of one of my buttons is not updating after being tapped. I have had no trouble doing the exact same thing in my other views, but here it simply doesn't work. The "heart" button will keep its original appearance (in this case an unfilled heart) no matter what, despite my print statements indicating that the value has changed. The other actions performed when tapping the heart work perfectly. I've been at it for hours.
Any help would be appreciated. Thanks!
import SwiftUI
struct SearchView: View {
@State private var searchText = ""
@StateObject var save = SaveWords()
@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()
}
}
}
#Preview {
SearchView()
}
Hi, I want to add a SwiftData model to my Swift Playground in Xcode 15.2, but I get errors related to iOS version, has anyone encountered this? Thanks
import Foundation
import SwiftData
struct testStruct: Identifiable {
let id = UUID()
var question: String
var answer: String
}
@Model
class testClass: Identifiable {
let id = UUID()
var question1: testStruct
var question2: testStruct
var question3: testStruct
var question4: testStruct
var question5: testStruct
init(question1: testStruct, question2: testStruct, question3: testStruct, question4: testStruct, question5: testStruct) {
self.question1 = question1
self.question2 = question2
self.question3 = question3
self.question4 = question4
self.question5 = question5
}
}
Post not yet marked as solved
Is it permissible to resubmit my previously rejected Swift Student Challenge project this year? I plan to make substantial changes, adding more functionality to enhance interactivity. I truly believe that my project idea has the potential to win, now that I have had the opportunity to learn more about Swift and SwiftUI over the past year.
Post not yet marked as solved
Your app playground must be built with and run on Swift Playgrounds 4.4 or later (requires iPadOS 16 or macOS 13.5, or later) or Xcode 15 on macOS 13.5, or later. You may incorporate the use of Apple Pencil.
If I want to develop a playground for the iPad and Apple Pencil, am I limited to using Playgrounds on iPadOS to make it? Or could I also use Playgrounds on macOS and Xcode?
If I use Xcode to make my iPad playground, should I select "Xcode" for "Which software should we use to run your app playground?" I assume so, as I used Xcode to make my app — but then it will be run in a simulator by the judges, which means they cannot use the Apple Pencil.
I'm a little lost here. Any guidance is appreciated.
Post not yet marked as solved
I have a few questions about the Swift Student Challenge development tools.
What is the difference between swift playground and Xcode playground?
Also, is it possible to develop vision OS in the playground?
Post not yet marked as solved
I am making a code that uses pencilKit and you can draw on the canvas. However, I want to be able to detect if the resulted drawing is in contact with a Rectangle().
Is there any way to do this?
I dont want to use variables for X and Y positions because I have more than 400 rectangles in a grid.
Hi, Developers. The project I am working on includes SwiftData for storing information. When inspecting, code-wise everything looks as it should, however, sometimes when interacting with data, the app crashes. The confusing part is that it happens sometimes. On one simulator, it might crash, while on another it might not. Should I reinstall the app it seems to be working fine again. Do you have and ideas. Any advice is appreciated. Take care!
Post not yet marked as solved
Is it possible to create SwiftData Document-based apps in App Playgrounds (or Swift Packages, since App Playgrounds are a special type of packages)?
As explained in wwdc2023-10154, to create a document-based app you are required to provide an UTType that conforms to com.apple.package. This requires your file type to be declared and exported in the Info.plist file of an Xcode project, but App Playgrounds don't have them.
Providing .package as a UTType seems to partially work:
import SwiftUI
import SwiftData
@main
struct SwiftDataFlashCardSample: App {
var body: some Scene {
#if os(iOS) || os(macOS)
DocumentGroup(editing: Card.self, contentType: .package) {
ContentView()
}
#else
WindowGroup {
ContentView()
}
.modelContainer(for: Card.self)
#endif
}
}
This way I am able to run the app, create and edit new document, and save it on disk. However, opening it again does not work (you cannot select it since it is a folder without a defined package type).
Is there any workaround to this?
I guess I could drop support for multiple documents entirely if this is not possible (I don't think this would affect my submission), but I would just like to check if there is any way to do this first.
Post not yet marked as solved
I noticed updating a @Transient attribute's value does not refresh SwiftUI views when using a SwiftData model with @Query or @Bindable.
Here is a sample code (clicking the button changes the Transient attribute):
import SwiftUI
import SwiftData
// SwiftUI App struct declaration
@main
struct MyApp: App {
var body: some Scene {
WindowGroup {
ContentView()
}
.modelContainer(for: MyModel.self)
}
}
// My SwiftData test model
@Model
class MyModel {
var myFirstArray: [Int]
@Transient var mySecondElement: Bool = false
init(myFirstArray: [Int] = [Int](), mySecondElement: Bool = false) {
self.myFirstArray = myFirstArray
self.mySecondElement = mySecondElement
}
}
// The SwiftUI View
struct ContentView: View {
@Query var myModels: [MyModel]
@Environment(\.modelContext) var modelContext
var body: some View {
VStack {
if myModels.count != 0 {
Text("Model:")
// I expected this to update when the button is clicked
Text("mySecondElement's value: \(myModels[0].mySecondElement ? "True" : "False" )")
Button {
// button that changes the transient attribute
myModels[0].mySecondElement.toggle()
print(myModels[0].mySecondElement) // this prints the actual value
} label: {
Text("Add mySecondElement data")
}
}
}
.onAppear {
try? modelContext.delete(model: MyModel.self)
if myModels.count == 0 {
let model = MyModel(myFirstArray: [0, 1, 3])
modelContext.insert(model)
}
}
}
}
I expected ContentView() to be refreshed when I clicked on the button. However, this seems to not be the case - it seems that attributes marked with @Transient don't publish updates to Views.
Is this expected behaviour? Should I file a bug / feedback for this?
Do the students have a specific topic for the app they are going to make? (If so, will it be given on February 5, 2024?)
The students are expected to submit their apps within three weeks from February 5, 2024, am I correct?
The apps the students are creating are expected to be "iPhone" Swift Playground apps, am I correct?
Do the students have a specific topic for the app they are going to make? (If so, will it be given on February 5, 2024?)
The students are expected to submit their apps within three weeks from February 5, 2024, am I correct?
The apps the students are creating are expected to be "iPhone" Swift Playground apps, am I correct?
Post not yet marked as solved
Hello,
Very recently, the following code has automatically appeared at the bottom of three of my SwiftUI View files:
@available(iOS 17.0, macOS 14.0, tvOS 17.0, visionOS 1.0, watchOS 10.0, *)
struct $s10Accent_Ace33_0BADA584A03144EFDAB57154E6FD3FBALl7PreviewfMf_15PreviewRegistryfMu_: DeveloperToolsSupport.PreviewRegistry {
static let fileID: String = "Accent_Ace/HistoryView.swift"
static let line: Int = 47
static let column: Int = 1
static func makePreview() throws -> DeveloperToolsSupport.Preview {
DeveloperToolsSupport.Preview {
let randomWord1 = FetchWord.getRandomWord()
let randomWord2 = FetchWord.getRandomWord()
@State var randomWords = [Word(word: randomWord1.0, IPA: randomWord1.1, lineNumber: randomWord1.2), Word(word: randomWord2.0, IPA: randomWord2.1, lineNumber: randomWord2.2)]
HistoryView(words: $randomWords)
}
}
}
This is from one of my files but it's very similar in the other two. It seems to have something to do with my previews.
The problem is that this code generates an error: Ambiguous use of 'init(_:traits:body:)'. My previews worked just fine before the auto-generated code appeared, so I tried deleting it. However, it automatically comes back no matter how many times I get rid of it. It's preventing me from building my App Playground, so I can't run the app or even see the previews. Does anyone know how to get rid of it or fix the error?
Thanks for the help!
Hi, I am participating in the swift student challenge, and I need some help.
When I make a color by Color(red: , green: , blue: ), I calculate it by getting the rgb of the color that I want and dividing each of the colors by 255. For example, if red was 5, I would divide it by 255 to get 0.01960784313, which I put in the code for the red parameter. However, when I run the code, the color that appears is a bit different than the one I was expecting to see. Can someone please explain this?
Post not yet marked as solved
I don't really understand the exact meaning of "Build your app playground" on the Apple Swift Student Challenge home page. Does it mean to build a game app with a normal Xcode SwiftUI project?
Or is there a platform called "App Playground"?
So we cannot make an app that is not a game or an app that is not made with a platform called "App Playground" ?????????
I'm confused.
If you can use Japanese, it would be helpful to answer in Japanese.
Hello, is the new Journaling Suggestions API available in App Playgrounds (made using Xcode)?
I know I can add capabilities in Xcode, but it seems that the Journaling Suggestions capability isn't available. I assume this API is currently unsupported in App Playgrounds?
Post not yet marked as solved
Hey, I am the winner of WWDC20 Swift Student Challenge, I was recently checking out my account hoping to find Events tab to find my winner badge there, but the whole section is gone. I was wondering if anyone can help me in finding out the updated or similar version of the Events section in this website :)