Am getting my data in AssetList Variable in ModelView File But in View File getting nil
** Model View File **
- In this file I have data in AssetList *
import SwiftUI
class FloatingPanelViewModel: ObservableObject {
typealias HomeAPICallback = (Bool,String) -> Void
var completion : HomeAPICallback?
// var arrayCategoryModel = pListManager.shared.homeOptionsItems
var AssetsList = [Asset]()
func getAssets(completion: @escaping HomeAPICallback) {
self.completion = completion
AssetsRequest.shared.getAssetsList(delegate: self)
}
}
extension FloatingPanelViewModel:ServiceRequestDelegate {
func successWithdata(response: BaseResponse) {
if response.isSuccess {
if response.requestType == APIRequests.RType_GetAssets {
if let responseData = response.serverData as? [String:AnyObject] {
do {
AssetsList.removeAll()
print(responseData)
if let AssetData = responseData["assets"] as? [AnyObject] {
let jsonData = try JSONSerialization.data(withJSONObject: AssetData, options: .prettyPrinted)
AssetsList = try JSONDecoder().decode([Asset].self, from: jsonData)
// for i in AssetsList {
// print(i)
// }
}
completion?(true,"")
}
catch {
failureWithdata(response: response)
}
}
}
} else {
failureWithdata(response: response)
}
}
func failureWithdata(response: BaseResponse) {
completion?(false,response.message)
}
}
** but in view getting nil **
- in this file am getting nil after assigning viewModel.AssetsList to Assets *
import SwiftUI
struct FloatingPanelContent: View {
// MARK: - PROPERTIES
@ObservedObject var viewModel = FloatingPanelViewModel()
@State var Assets: [Asset] = []
init() {
viewModel.getAssets { (status, message) in
print(status)
print(message)
}
self.Assets = viewModel.AssetsList
}
// MARK: - BODY
var body: some View {
ScrollView(.vertical, showsIndicators: false){
VStack(alignment: .center, spacing: 10){
ForEach(viewModel.AssetsList) { asset in
// print(asset)
AssetsView(asset: asset)
.cornerRadius(12)
} //: LOOP
}
} // Scroll View
}
[Assets Variable print in View File ][1]
- ScreenShot of Printing AssetsList Variable in ViewModel*
- ScreenShot of Printing Assets Variable in View *