Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

'ContentView_Previews' is not a member type of error

Tags:

swiftui

'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'

like image 370
Tim Avatar asked Sep 12 '19 21:09

Tim


People also ask

What is instance member in Swift?

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.

Which one of the key Cannot be used with instance variable?

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.


1 Answers

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.

like image 170
Louis Lac Avatar answered Sep 22 '22 19:09

Louis Lac