Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Instance member cannot be used on type in SwiftUI Preview

Tags:

Following error in my preview:

struct DetailView: View {     var header: DataProvider.DataHeader      var body: some View {         Text("...")     } }  struct DetailView_Previews: PreviewProvider {     var a = DataProvider.DataHeader(title: "a", text: "b")      static var previews: some View {         DetailView(header: a)     } } 

Error is:

Instance member 'a' cannot be used on type 'DetailView_Previews' 

Why this is happening?

like image 633
gurehbgui Avatar asked May 12 '20 13:05

gurehbgui


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

It is due to static var preview,

so use either static as well

static var a = DataProvider.DataHeader(title: "a", text: "b") 

or construct in place

DetailView(header: DataProvider.DataHeader(title: "a", text: "b")) 
like image 121
Asperi Avatar answered Sep 29 '22 05:09

Asperi