Unable to create list of string from json response

I have a list of remarks which I receive from an API call. I have created a loop to create TEXT from the remarks but the texts do not appear in the application. Please check the code.
I have also checked the debugger and it shows a the string present there but it doesn't create the text.

Code Block
VStack{
Text("Remarks")
.padding(.horizontal, 15)
.font(.title2)
if(currentFrResponse.remarks != nil){
List(currentFrResponse.remarks!, id:\.self){ currentRemark in
VStack{
Text(currentRemark)
.bold()
.padding()
}.padding()
}
}
}

Let me know if you need any more details
Answered by Tahreem in 671627022
In the end i used a ForEach loop for this. I actually wished to create a list of textfields which could be add on the basis of the remarks received from the API call. Also this list needed to be dynamic, i.e, I could add or remove the textfields dynamically by using buttons which is why I wanted to add a list...

Unfortunately I couldn't. But now I created a a for loop and then added two buttons which could add or remove the textfields and it now works perfectly.

Here is the code for this.
Code Block
ForEach(0..<remarksList.count, id: \.self) { index in
TextField("Remarks", text: Binding<String>(
get: {remarksList[index] }, set: {remarksList[index] = $0}) )
.padding()
.background(Color("light_gray"))
.foregroundColor(.black)
.cornerRadius(8)
.disabled(!showUpdateButton)
}

Code Block
if showUpdateButton {
HStack{
Button(action:{
self.remarksList.append("")
}) {
Text("Add Remarks")
}
Spacer()
Button(action:{
_ = self.remarksList.popLast()
}) {
Text("Delete Remarks")
}
}.padding(30)
}

Let me know if you need any more details

Please show as much details as you can. Especially your code lacks the definition of currentFrResponse and no code to update it is shown. Far from saying something.
First thing to check is whether or not currentFrResponse.remarks is nil.

A simple way to do this is to comment out line 7.
If the app crashes, that means it was nil.
@OOPer the "currentFrResponse" is an object of struct pasted below:

Code Block struct CurrentFrResponse: Codable {
    var frId : String?
    var clientFrId : String?
    var customerRefId : String?
    var requestorName : String?
    var requestorContactNo : String?
    var location : Location?
    var building : Building?
    var division : Division?
    var locationDesc : String?
    var faultCategory : Division?
    var priority : Division?
    var department : Division?
    var maintGrp : Division?
    var remarks : [String]?
    var purchaseOrder : String?
    var attendedBy : [AttendedBy]?
}

I have removed some properties from this struct to make it simple.

I have also checked the debugger and it shows a the string present

present where ? In currentFrResponse.remarks! ?

@OOPer the "currentFrResponse" is an object of struct pasted below:

The first line is missing. And?


Can you show buildable code? From imports till end of file.

currentFrResponse is a struct or a class? It is declared as @State or @StateObject?

Please show all such things.
This code runs quite fine and shows 3 rows A, B and C below the title Remarks:
Code Block
import SwiftUI
struct MyView: View {
@State var currentFrResponse: CurrentFrResponse = CurrentFrResponse.createTestData()
var body: some View {
VStack{
Text("Remarks")
.padding(.horizontal, 15)
.font(.title2)
if(currentFrResponse.remarks != nil){
List(currentFrResponse.remarks!, id:\.self){ currentRemark in
VStack{
Text(currentRemark)
.bold()
.padding()
}.padding()
}
}
}
}
}
struct MyView_Previews: PreviewProvider {
static var previews: some View {
MyView()
}
}
extension CurrentFrResponse {
static func createTestData() -> CurrentFrResponse {
var response = CurrentFrResponse()
response.remarks = [
"A", "B", "C"
]
return response
}
}

So, it is clear that your problem exists in somewhere else not shown.
@OOPer

I don't know what the issue is. If I only change the 'List' to 'ForEach' in the above shown code.... it prints out the remarks but doesn't work for 'List'.

but doesn't work for 'List'.

If you could show enough info about your doesn't work case, someone would be able to help solving it.
Accepted Answer
In the end i used a ForEach loop for this. I actually wished to create a list of textfields which could be add on the basis of the remarks received from the API call. Also this list needed to be dynamic, i.e, I could add or remove the textfields dynamically by using buttons which is why I wanted to add a list...

Unfortunately I couldn't. But now I created a a for loop and then added two buttons which could add or remove the textfields and it now works perfectly.

Here is the code for this.
Code Block
ForEach(0..<remarksList.count, id: \.self) { index in
TextField("Remarks", text: Binding<String>(
get: {remarksList[index] }, set: {remarksList[index] = $0}) )
.padding()
.background(Color("light_gray"))
.foregroundColor(.black)
.cornerRadius(8)
.disabled(!showUpdateButton)
}

Code Block
if showUpdateButton {
HStack{
Button(action:{
self.remarksList.append("")
}) {
Text("Add Remarks")
}
Spacer()
Button(action:{
_ = self.remarksList.popLast()
}) {
Text("Delete Remarks")
}
}.padding(30)
}

Unable to create list of string from json response
 
 
Q