Home > Net >  Ways to access Model's variables in SwiftUI?
Ways to access Model's variables in SwiftUI?

Time:08-11

Hi so I have a struct modal like following:

struct calmDocc: Identifiable, Codable, Hashable {

var content: String
var id: String
var calmwordcount: Int
  }

I assign values to these inside some class (CalmManager) as follows

 let idcalm = something
                    let content = something as! String ?? ""
                    let calmwordcount = content.split { !$0.isLetter }.count
                    let calmDoccs = calmDocc(content:content, id:idcalm, calmwordcount: calmwordcount)

                    return (calmDoccs)

Now when I wish to call the calmwordcount var of CalmDoccs in some other view I have to use the following

ForEach(CalmManager.calmDoccs) { calmDoccs in
          
            Text(calmDoccs.calmwordcount.description)
        
        }

Is there some simple way to basically target one of the three variables I have defined inside calmDocc without using the ForEach command? Something like following "calmDoccs.content"? Would appreciate some help.

EDIT:

     self.calmDoccs = documents.map { (queryDocumentSnapshot) -> calmDocc in
                   let data = queryDocumentSnapshot.data()
                    let idcalm = queryDocumentSnapshot.documentID
                    let content = data["content"] as! String ?? ""
                    let calmwordcount = content.split { !$0.isLetter }.count
                    let calmDoccs = calmDocc(content:content, id:idcalm, calmwordcount: calmwordcount)
                    self.objectWillChange.send()
                    print(calmDoccs)
                    return (calmDoccs)
                }

EDIT 2: How I define class calmManager

class calmManager: ObservableObject {


@Published var calmDoccs: [calmDocc] = []
@Published var calmContent: String = ""
@Published var idcalm: String = ""
 //some code later down the line }

EDIT 3: This is what I get when I'm trynna add the word counts:

What I use: if CalmManager.calmDoccs.count > 0 && DarkManager.darkDoccs.count > 0{ Text("count: \(CalmManager.calmDoccs[0].calmwordcount) \(DarkManager.darkDoccs[0].darkwordcount)") }

What I get: enter image description here

What I wish I would get:

count :10

CodePudding user response:

Here is some code that may help you.

import Foundation
import SwiftUI

struct ContentView: View {
    var body: some View {
        TestView()
    }
}

struct CalmDocc: Identifiable, Codable, Hashable {
    var content: String
    var id: String
    var calmwordcount: Int
}

class CalmManager: ObservableObject {
    // for testing
    @Published var calmDoccs: [CalmDocc] = [
        CalmDocc(content: "content-1", id: "id-1", calmwordcount: 1),
        CalmDocc(content: "content-2", id: "id-2", calmwordcount: 2),
        CalmDocc(content: "content-3", id: "id-3", calmwordcount: 3)
    ]
    @Published var calmContent: String = ""
    @Published var idcalm: String = ""
}

struct TestView: View {
    @StateObject var manager = CalmManager()
    
    var body: some View {
        List {
            ForEach(manager.calmDoccs) { calmDocc in
                VStack {
                    Text(calmDocc.id)
                    Text(calmDocc.content)
                    Text("count: \(calmDocc.calmwordcount)")
                }
            }
        }
    }
}

You should read again the basics at: https://docs.swift.org/swift-book/LanguageGuide/TheBasics.html and in particular, how to use an array, and collections in general: (https://docs.swift.org/swift-book/LanguageGuide/CollectionTypes.html). I recommend you also do the tutorial at: https://developer.apple.com/tutorials/swiftui/

EDIT-1: to show only a specific element of calmDoccs array:

struct TestView: View {
    @StateObject var manager = CalmManager()
    
    var body: some View {
        if manager.calmDoccs.count > 0 {
            Text(manager.calmDoccs[0].id)
            Text(manager.calmDoccs[0].content)
            Text("count: \(manager.calmDoccs[0].calmwordcount)")
        }
    }
}

EDIT-2: to get totalCount displayed.

struct TestView: View {
    @StateObject var manager1 = CalmManager()
    @StateObject var manager2 = CalmManager()
    
    var body: some View {
        if manager1.calmDoccs.count > 0 && manager2.calmDoccs.count > 0 {
            let totalCount = manager1.calmDoccs[0].calmwordcount   manager2.calmDoccs[0].calmwordcount
            Text("total count: \(totalCount)")

            // yes, you can also do this, pay attention to the details
            Text("total count: \(manager1.calmDoccs[0].calmwordcount   manager2.calmDoccs[0].calmwordcount)")

        }
    }
}
  • Related