Is there anyone I could save data to a device with Swift, like how we have cookies for browsers?
Selecting any option will automatically load the page
Post
Replies
Boosts
Views
Activity
This is my code (note that this isn't the whole code):
struct GrowingButton: ButtonStyle {
func makeBody(configuration: Configuration) -> some View {
configuration.label
.padding()
.background(Color.blue)
.foregroundColor(.white)
.clipShape(Capsule())
.scaleEffect(configuration.isPressed ? 1.2 : 1)
.animation(.easeOut(duration: 0.2), value: configuration.isPressed)
}
}
struct multiplicationcheck: View {
var body: some View {
if (startgame == true) {
let confirmation = checkanswer(guess: username, aanswer: answer1 as! Int)
moreText = confirmation
}
if (page == "multiplication stage1"){
didfunc = genquesmulti()
answer1 = didfunc[0]
mainquestion = didfunc[1] as! String
} else if (page == "multiplication stage2"){
didfunc = genquesmulti2()
answer1 = didfunc[0]
mainquestion = didfunc[1] as! String
} else if (page == "multiplication stage3"){
didfunc = genquesmulti3()
answer1 = didfunc[0]
mainquestion = didfunc[1] as! String
} else if (page == "multiplication stage4"){
didfunc = genquesmulti4()
answer1 = didfunc[0]
mainquestion = didfunc[1] as! String
}
username = ""
startgame = true
}
}
struct additioncheck: View {
var body: some View {
if (startgame == true) {
let confirmation = checkanswer(guess: username, aanswer: answer1 as! Int)
moreText = confirmation
}
if (page == "addition stage1"){
didfunc = genquesadd()
answer1 = didfunc[0]
mainquestion = didfunc[1] as! String
} else if (page == "addition stage2"){
didfunc = genquesadd2()
answer1 = didfunc[0]
mainquestion = didfunc[1] as! String
} else if (page == "addition stage3"){
didfunc = genquesadd3()
answer1 = didfunc[0]
mainquestion = didfunc[1] as! String
} else if (page == "addition stage4"){
didfunc = genquesadd4()
answer1 = didfunc[0]
mainquestion = didfunc[1] as! String
} else if (page == "addition stage5"){
didfunc = genquesadd5()
answer1 = didfunc[0]
mainquestion = didfunc[1] as! String
} else if (page == "addition stage6"){
didfunc = genquesadd6()
answer1 = didfunc[0]
mainquestion = didfunc[1] as! String
} else if (page == "addition stage7"){
didfunc = genquesadd7()
answer1 = didfunc[0]
mainquestion = didfunc[1] as! String
}
username = ""
startgame = true
}
}
struct divisioncheck: View {
var body: some View {
if (startgame == true) {
let confirmation = checkanswer(guess: username, aanswer: answer1 as! Int)
moreText = confirmation
}
didfunc = genquesdivi()
answer1 = didfunc[0]
mainquestion = didfunc[1] as! String
username = ""
startgame = true
}
}
struct textquestion: View {
var body: some View {
Text(moreText).font(.body)
.padding(1).foregroundColor(.black).font(Font.system(.body, design: .rounded))
Text(mainquestion).padding().foregroundColor(.black).font(Font.system(.largeTitle, design: .rounded))
}
}
func reset(){
moreText = ""
timeRemaining = 60
score = 0
overall = 0
gameover = false
answer1 = 0
mainquestion = "Press submit to start"
startgame = false
}
struct ContentView: View {
var timer2 = Timer.publish(every: 1, on: .main, in: .common).autoconnect()
let bg = LinearGradient(
gradient: Gradient(colors: [Color.pink, Color.yellow]),
startPoint: .top, endPoint: .bottom)
var body: some View {
ZStack { // 1
bg.ignoresSafeArea()
VStack {
if gameover {
gameoverscreen()
// some more code
} else if (page == "multiplication stage1" || page == "multiplication stage2" || page == "multiplication stage3" || page == "multiplication stage4") {
let stage = page.replacingOccurrences(of: "multiplication stage", with: "")
Text("Multiplication - Stage \(stage)").foregroundColor(.black).font(Font.system(.title, design: .rounded))
TextField(
"Answer",
text: username
).keyboardType(.numberPad).padding(10)
.font(Font.system(size: 15, weight: .medium, design: .rounded))
.overlay(RoundedRectangle(cornerRadius: 10).stroke(Color.blue, lineWidth: 1)).foregroundColor(.black)
.onSubmit {
multiplicationcheck()
}
Button("Submit") {
multiplicationcheck()
}
.buttonStyle(GrowingButton())
} else if (additionvalues.contains(page)) {
TextField(
"Answer",
text: username
).keyboardType(.numberPad).padding(10)
.font(Font.system(size: 15, weight: .medium, design: .rounded))
.overlay(RoundedRectangle(cornerRadius: 10).stroke(Color.blue, lineWidth: 1)).foregroundColor(.black)
.onSubmit {
additioncheck()
}
Button("Submit") {
additioncheck()
}
.buttonStyle(GrowingButton())
} else if (page == "division stage1"){
TextField(
"Answer",
text: username
).keyboardType(.numberPad).padding(10)
.font(Font.system(size: 15, weight: .medium, design: .rounded))
.overlay(RoundedRectangle(cornerRadius: 10).stroke(Color.blue, lineWidth: 1)).foregroundColor(.black)
.onSubmit {
divisioncheck()
}
Button("Submit") {
divisioncheck()
}
.buttonStyle(GrowingButton())
}
}
}
}
}
struct ContentView_Previews: PreviewProvider {
static var previews: some View {
Group {
ContentView()
}
}
}
In the lines var body: some View {, in the ContentView struct, multiplicationcheck struct and additioncheck struct, I'm getting the error The compiler is unable to type-check this expression in reasonable time; try breaking up the expression into distinct sub-expressions. I understand why this is happening, but not how to fix it.
In the line var body: some View { in the divisioncheck struct, I am getting the error Type '()' cannot conform to 'View'. I don't understand why this error is happening.
If you could help me fix this, that would be great.
Thanks!
So I've been struggling with an error which shows that the code is too long to process, so I decided to split a View struct into multiple some Views.
That hasn't worked well as it shows even more errors.
Here is some of the code:
struct ContentView: View {
var timer2 = Timer.publish(every: 1, on: .main, in: .common).autoconnect()
let bg = LinearGradient(
gradient: Gradient(colors: [Color.pink, Color.yellow]),
startPoint: .top, endPoint: .bottom)
if gameover {
var body: some View {
ZStack { // 1
bg.ignoresSafeArea()
VStack {
gameoverscreen()
}
}
}
} else if (page == "main") {
var body: some View {
ZStack { // 1
bg.ignoresSafeArea()
VStack {
mainpage()
}
}
}
} else if (page == "multiplication game") {
var body: some View {
ZStack { // 1
bg.ignoresSafeArea()
VStack {
multiplicationgame()
}
}
}
} else if (page == "addition game") {
var body: some View {
ZStack { // 1
bg.ignoresSafeArea()
VStack {
additiongame()
}
}
}
}
On the first line, it shows the error: Type 'ContentView' does not conform to protocol 'View'.
On the if gameover { line, it shows the error: Expected declaration
Is there a way I can fix it, and if there is, how?
Thanks
import SwiftUI
var weapons = ["lightsabre", "gun", "sword", "dagger", "metal stick"]
var weapon:String = "fist"
var diamonds:Int = 0
var jailtime:Int = 5
var gun:Int = 0
var vault:Int = 0
var goojfc:Int = 0
var health:Int = 50
var page:String = "main"
struct main: View {
var body: some View {
Text("Welcome to Diamond Risk!" + page)
Button("Play"){
page = "play"
}
Button("Rules"){
page = "rules"
}
}
}
struct rules: View {
var body: some View {
Text("Welcome to Diamond Risk!")
Button("Home Page"){
page = "main"
}
Button("Play"){
page = "play"
}
}
}
struct ContentView: View {
var body: some View {
Text(page)
if (page == "main") {
main()
} else if (page == "rules") {
rules()
}
}
}
struct ContentView_Previews: PreviewProvider {
static var previews: some View {
ContentView()
}
}
I used this exact code for a project before, and it worked fine.
But now the variable page is just not changing.
How do I fix it so it works and changes?