Swift: Value of type ‘Published.Publisher’ has no subscripts
Issue
If you face Value of type 'Published<Text>.Publisher' has no subscripts
error, then you are probably trying to access the variable wrong way. The API has changed since beta when it was first released – a lot has changed since then, causing the issues you’re experiencing. With that being said, the breaking changes introduced is not difficult to resolve.
Solution
Depending on your use case you have to access the value with or without $sign
, whether you want to access value or binding.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import SwiftUI | |
class SomeClass: ObservableObject { | |
@Published var someVariable: Text = "Hello World" | |
} | |
struct ContentView: View { | |
@EnvironmentObject var someClass: SomeClass // This is your @Published var modals: [Modal] = [] | |
var body: some View { | |
return ZStack { | |
Text(someClass.someVariable) // we're accessing a Text value | |
AnotherView(bindingValue: $someClass.someVariable) // we're accessing a Binding<Text> value | |
} | |
} | |
} |
iOS Elements
You can find also the whole implementation in my UI kit — Download below.
Use discount code MOLCIK to get iOS Elements for $29.70 (70% off)