'ContentView_Previews' does not compile if ContentView references an external object.
If I remove all references to @ObservedObject, preview compiles.
import SwiftUI
struct ContentView: View {
@ObservedObject var fancyTimer = FancyTimer()
var body: some View {
Text("\(fancyTimer.timerValue)")
.font(.largeTitle)
}
}
struct ContentView_Previews: PreviewProvider {
static var previews: some View {
ContentView()
}
}
import Foundation
import SwiftUI
import Combine
class FancyTimer: ObservableObject {
@Published var timerValue: Int = 0
init() {
Timer.scheduledTimer(withTimeInterval: 1.0, repeats: true)
{ timer in
self.timerValue += 1
}
}
}
Error is: 'ContentView' is not a member type of 'FancyTimer'
Unless otherwise specified, a member declared within a class is an instance member. So instanceInteger and instanceMethod are both instance members. The runtime system creates one copy of each instance variable for each instance of a class created by a program.
Instance Variable cannot have a Static modifier as it will become a Class level variable. Meaning STATIC is one of the keyword which cannot be used with Instance variable.
Often the problem is that you created a class, a struct, or enum that has the same name as the module you are in.
Here, odds are that "FancyTimer" is also the name of your project, which triggers the error.
Try to change the class name.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With