-1

I have another project with almost identical code and it runs with no issues i'm not sure why it crashes. It build successfully then it crashes with Fatal error on the if statement line.

     if sharedData.likedAwards.isEmpty {
                        
                        Group {
                            Image("NoAwards")
                                .resizable()
                                .aspectRatio(contentMode: .fit)
                                .padding()
                                .padding(.top,35)
                        
                            Text("No awards yet")
                                .font(.custom(customFont, size: 25))
                                .fontWeight(.semibold)
                            
                        
                        }

The Following Code is in SharedDataModel:

import SwiftUI

class SharedDataModel: ObservableObject {
    
    // Detail Award Data....
    @Published var detailAward: Award?
    @Published var showDetailAward: Bool = false
    
    // matched Geoemtry Effect from Search page...
    @Published var fromSearchPage: Bool = false
    
    // Liked Awards...
    @Published var likedAwards: [Award] = []
    
    
}
Mxyb
  • 83
  • 1
  • 9

1 Answers1

1

use @StateObject var sharedData = SharedDataModel() to create instances of your object on the view to be conform

struct ContentView : View {

@StateObject var sharedData = SharedDataModel()

var body: some View {
Vstack{
if sharedData.likedAwards.isEmpty {
                    
                    Group {
                        Image("NoAwards")
                            .resizable()
                            .aspectRatio(contentMode: .fit)
                            .padding()
                            .padding(.top,35)
                    
                        Text("No awards yet")
                            .font(.custom(customFont, size: 25))
                            .fontWeight(.semibold)
                        
                    }

                    }
}
AdR
  • 572
  • 1
  • 4
  • 13
  • Thanks, this prevents the crash. I am now trying to display the MainPage when the application is launched. If the: if sharedData line is removed from ContentView the crash happens however I need the MainPage to appear. I'm struggling to make this happen without the app crashing. Any ideas? Thanks – Mxyb Feb 18 '22 at 14:23
  • I don’t know if you can share your code with GitHub or something. If you want me to help you – AdR Feb 18 '22 at 15:42
  • that sounds good thanks. I just set up my GitHub account. I assume I just publish repository and make it public? My username is: Mxyb I just published the repository. Thanks a lot – Mxyb Feb 19 '22 at 14:47
  • there is nothing in your repo project – AdR Feb 19 '22 at 16:24
  • I published the repository on GitHub Desktop and when I go on the website it is there for me and says public. What do I need to do so that you can view it? Thanks – Mxyb Feb 19 '22 at 16:40
  • I don't use github desktop but yes you have to stay in public for me to have access to the repo – AdR Feb 19 '22 at 16:47
  • It's titled CollectionPageV1 and is public, is it working now? – Mxyb Feb 19 '22 at 17:14
  • I can see only your initial commit. You must do « git add . » git commit -m « message » and git push to your repo – AdR Feb 19 '22 at 17:31
  • If your are Twitter follow me @SrgAdr it will be more simple to send message – AdR Feb 19 '22 at 17:42
  • Okay thanks! I followed you on twitter – Mxyb Feb 20 '22 at 11:51