Hello. I am fairly new to SwiftUI, and I am currently making a timetable app, to help keep me organised, next academic year, so I still have a fair amount of time to complete it, before September.
I am making this app specifically for my college, and its timetabling. There are 6 periods, A-F, and every A period, you have the same lesson, every B period, etc. Parts of this project come from tutorials I have worked through, and real life examples I have looked at.
It is not at all elegant, or working, currently.
The view I am having a problem with is the so-called 'TimetableEditView'. I want a user to choose a subject from the list, for all of their periods, and then save them. These will then be used in the actual timetable.
The error I am getting, is at both lines 21 & 22.
Line 21 error: 'Cannot assign value of type 'Int' to type 'String''
Line 22 error: 'Cannot assign value of type 'String' to type 'Int''
import SwiftUI
struct TimetableEditView: View {
var subjects = ["Free", "Maths", "English", "Geography"]
@State var selectedSubjectA = 0
...
@State var retrievedSubjectA = ""
...
var body: some View {
Form {
Section {
Picker(selection: $selectedSubjectA, label: Text("A Periods").font(.body)) {
ForEach(0 ..< subjects.count) {
Text(self.subjects[$0]).font(.body)
}
}
}
Button(action: {
UserDefaults.standard.set(self.selectedSubjectA, forKey: "SelectedSubjectA")
self.retrievedSubjectA = self.selectedSubjectA
self.selectedSubjectA = ""
UserDefaults.standard.set(self.selectedSubjectB, forKey: "SelectedSubjectB")
...
}) {
Text("Save")
.font(.headline)
.fontWeight(.bold)
.foregroundColor(.blue)
}
.onAppear {
guard let retrievedSubjectAa = UserDefaults.standard.value(forKey: "SelectedSubjectA") else {return}
self.retrievedSubjectA = retrievedSubjectAa as! String
}
}
.navigationBarTitle("Select your subjects", displayMode: .inline)
}
}
I understand that this is because: selectedSubjectA = 0
for the purposes of choosing the name from the list of subjects, and because: retrievedSubjectA = ""
which I believe means that it is a string.
I want to assign the users chosen subject to a variable, that I can then use in different view.
I understand that this is quite basic compared to most questions on here, but any and all help is appreciated.
Thanks.
Post
Replies
Boosts
Views
Activity
Hello! I am wanting to have the current time updating in SwiftUI I want to embed this in another view. After scouring the internet, this is what I've managed to put together so far.
Here's where I've got so far:
struct Time : View {
static let timeFormatter: DateFormatter = {
let formatter = DateFormatter()
formatter.dateStyle = .none
formatter.timeStyle = .medium
return formatter
}()
var now = Date()
@State var timer: Timer
@State var repeater: Bool = true
var body: some View {
Text("\(now, formatter: Self.timeFormatter)")
}
}
}
I don't have loads of experience – any and all help is appreciated, and I thank-you all for your time.