Hi,
I am a new coder and I have 3 code errors in my app that I am making.
Which are
Value of type 'String' has no member 'frame' : line 174
Cannot infer contextual base in reference to member 'all': line 176
Closure containing a declaration cannot be used with function builder 'ViewBuilder' : line 186
Finally, here is my code:
import SwiftUI
struct ContentView: View {
@State private var currentCount = UserDefaults.standard.integer(forKey: "currentCount")
@State private var isDecrementOn = false
func performsMath(isSubtract: Bool, num: Int) {
var result = self.currentCount
if isSubtract==true{
result -= num
} else {
result += num
if result = 0 {
currentCount = result
UserDefaults.standard.set(currentCount, forKey: "currentCount")
}
}
}
var body: some View {
ScrollView {
VStack(alignment: .center, spacing: 10) {
HStack(alignment: .center, spacing: 20) {
Button(action: {
self.isDecrementOn.toggle()
}) {
Image(systemName: isDecrementOn ? "minus" : "plus")
.font(.headline)
.foregroundColor(.blue)
aspectRatio(contentMode: .fit)
}
Button(action: {
self.currentCount = 0
UserDefaults.standard.set(0,forKey: "currentCount")
}) {
Image(systemName: "trash")
.font(.headline)
.foregroundColor(.red)
aspectRatio(contentMode: .fit)
}
}
Button(action: {
self.performsMath(isSubtract: self.isDecrementOn, num: 1)
}){
Text("\(self.currentCount)")
.font(.title)
}
}
ScrollView(.horizontal) {
HStack(alignment: .center, spacing: 10) {
Button(action: {
self.performsMath(isSubtract: self.isDecrementOn, num: 1)
}) {
Text(isDecrementOn ? "-1" : "+1")
}
Button(action: {
self.performsMath(isSubtract: self.isDecrementOn, num: 2)
}) {
Text(isDecrementOn ? "-2" : "+2"
) }
Button(action: {
self.performsMath(isSubtract: self.isDecrementOn, num: 3)
}) {
Text(isDecrementOn ? "-3" : "+3"
) }
Button(action: {
self.performsMath(isSubtract: self.isDecrementOn, num: 4)
}) {
Text(isDecrementOn ? "-4" : "+4"
) }
Button(action: {
self.performsMath(isSubtract: self.isDecrementOn, num: 5)
}) {
Text(isDecrementOn ? "-5" : "+5"
) }
Button(action: {
self.performsMath(isSubtract: self.isDecrementOn, num: 10)
}) {
Text(isDecrementOn ? "-10" : "+10"
.frame(width: 50)
.padding(.all)
) }
}
struct ContentView_Previews: PreviewProvider {
static var previews: some View {
ContentView() }
}
}
}
}}
Could someone help me?
Post
Replies
Boosts
Views
Activity
Hello, I am making an app for Apple watch but I have lots of errors.
My code is:
import SwiftUI
struct ContentView: View {
@State private var currentCount = UserDefaults.standard.integer(forKey: "currentCount")
@State private var isDecrementOn = false
var body: some View {
ScrollView {
VStack(alignment: .center, spacing: 10) {
HStack(alignment: .center, spacing: 20) {
Button(action: {
self.isDecrementOn.toggle()
}) {
Image(systemName: isDecrementOn ? "minus" : plus")
.font(.headline)
.foregroundColor(.blue)
aspectRatio(contentMode: .fit)
}
Button(action: {
self.currentCount = 0
UserDefaults.standard.set(0,forKey: "currentCount")
}) {
Image(systemName: "trash")
.font(.headline)
.foregroundColor(.red)
aspectRatio(contentMode: .fit)
}
}
Button(action: {
self.performsMath(isSubtract: self.isDecrementOn, num: 1)
})
Text("\(self.currentCount)")
.font(.title)
}
}
ScrollView(.horizontal){
HStack(alignment: .center, spacing: 10){
Button(action: {
self.performsMath(isSubtract: self.isDecrementOn, num: 1)
}
Text(isDecrementOn ? "-1" : "+1")
Button(action: {
self.performsMath(isSubtract: self.isDecrementOn, num: 2)
}
Text(isDecrementOn ? "-2" : "+2"
Button(action: {
self.performsMath(isSubtract: self.isDecrementOn, num: 3)
}
Text(isDecrementOn ? "-3" : "+3"
Button(action: {
self.performsMath(isSubtract: self.isDecrementOn, num: 4)
}
Text(isDecrementOn ? "-4" : "+4"
Button(action: {
self.performsMath(isSubtract: self.isDecrementOn, num: 5)
}
Text(isDecrementOn ? "-5" : "+5"
Button(action: {
self.performsMath(isSubtract: self.isDecrementOn, num: 10)
}
Text(isDecrementOn ? "-10" : "+10"
.frame(width: 50)
.padding(.all)
func performMaths(isSubtract: Bool, num: Int) {
var result = self.currentCount
if isSubtract==true{
result -= num
}else{
result += num
if result >= 0 {
currentCount = result
UserDefaults.standard.set(currentCount, forKey: "currentCount")
struct ContentView_Previews: PreviewProvider {
static var previews: some View {
ContentView()
}
}
}
}
The first error is unterminated string literal on: Image(systemName: isDecrementOn ? "minus" : plus")
The second error is expected separator for: .foregroundColor(.blue) but when I click fix the same error pops up somewhere else on the code.
Thats the same error on here.
ScrollView(.horizontal){
HStack(alignment: .center, spacing: 10){
Button(action: {
self.performsMath(isSubtract: self.isDecrementOn, num: 1)
} <- this line
Text(isDecrementOn ? "-1" : "+1")
The last errors are:
Expected '}' at end of brace statement
Expected '}' at end of closure
Expected '}' in struct
and they are at the end of my code but when I do what they ask the errors keep ******* up on the line underneath.
Could someone help me?
Thanks
Hi,
I am using Swift UI tutorial and now I am on Customize the Row Preview. This is my code:
LandmarkRow(landmark: landmarks[1])
I am getting an error "Could not find 'landmarks' in scope. Any suggestions?
Thanks!