Very new to coding and trying to teach myself swift. Running into a problem that I am having trouble understanding. Apologies if my code is a mess or my question is not clear.
I am creating a navigationview from a list I have created. I would like each list item to link to a detail page, that has a button opening a qlpreview of a word document, specific to that list item.
Firstly I have set the structure of my list items:
struct ScriptItem: Identifiable {
let id = UUID()
let ex: String
let phase: String
let doc: String
}
I then have my list and navigationview struct set up to link to a detailed view
struct ScriptsView: View {
@State private var queryString = ""
private let scriptList: [ScriptItem] = [
ScriptItem(ex: "101",
phase: "HMI",
doc: "PILOT NOTES EX 101"),
ScriptItem(ex: "102",
phase: "HMI",
doc: "PILOT NOTES EX 201")].sorted(by: {$0.ex < $1.ex})
var filteredScript: [ScriptItem] {
if queryString.isEmpty {
return scriptList
} else {
return scriptList.filter {
$0.ex.lowercased().contains(queryString.lowercased()) == true || $0.phase.lowercased().contains(queryString.lowercased()) == true
}
}
}
var body: some View {
NavigationView {
List(filteredScript) { scriptItem in
NavigationLink(destination: ScriptDetails(scriptitem: scriptItem)) {
HStack {
ZStack {
Text(scriptItem.ex)
}
Text(scriptItem.phase)
}
}
} .navigationBarTitle("Exercise Scripts")
} .searchable(text: $queryString, placement: .navigationBarDrawer(displayMode: .always), prompt: "Script Search")
}
}
My problem is in the next section - in the detail struct I'm trying to create a url link using forResource: but when I use the string from the previous struct I get the following error:
CANNOT USE INSTANCE MEMBER 'SCRIPTITEM' WITHIN PROPERTY INITIALIZER. PROPERTY INITIALIZERS RUN BEFORE 'SELF' IS AVAILABLE.
struct ScriptDetails: View {
let scriptitem: ScriptItem
let fileUrl = Bundle.main.url(
forResource: scriptitem.doc, withExtension: "docx"
)
ETC ETC ETC
Is there a way around this error that allows me to refer to the string I need from the previous struct?