-1

Why do I keep getting this error when trying to create a firebase counter? I am literally following the google firebase docs line for line. this is the create counter function

  func createCounter(ref: DocumentReference, numShards: Int) {
               ref.setData(["numShards": numShards]){ (err) in
                   for i in 0...numShards {
                       ref.collection("shards").document(String(i)).setData(["count": 0])
                   }
               }
           }

and this is how I am trying to use it

 Button("In there"){createCounter(ref: ref.document("Posts"), numShards: 0); incrementCounter(ref: ref.document("Posts"), numShards: 0); getCount(ref: ref.document("Posts"))
                        
                    }

I also keep getting this "Return from initializer without initializing all stored properties" error when I have this.

struct PostRow: View {
    
    var post: PostModel
    @ObservedObject var postData : PostViewModel
    let db = Firestore.firestore()
    let uid = Auth.auth().currentUser!.uid
    let numShards: Int
    let count: Int
    
    init(numShards: Int, count: Int) {
        self.numShards = numShards
        self.count = count
        
    }
Bstew
  • 1
  • 4

2 Answers2

0

Use this code in the init(){} of SwiftUI View.

init() {
      
        UINavigationBar.appearance().barTintColor = UIColor.clear        
        UINavigationBar.appearance().tintColor = .clear
        UINavigationBar.appearance().isOpaque = true

       }
Shahriar Nasim Nafi
  • 1,160
  • 15
  • 19
  • I want to show you my code but I don't know how to respond to your answer in a way that will let me paste it. I keep getting this error whenever I paste what you just posted, "Return from initializer without initializing all stored properties" – Bstew Jun 26 '21 at 02:31
  • I Updated my question to show you the problem. – Bstew Jun 26 '21 at 02:37
  • You need to initialize all your variable in init(){} . See @workingdog answer – Shahriar Nasim Nafi Jun 26 '21 at 09:11
0

you are getting the error because you are not initialising all your variables in "init(...)". Try something like this:

struct MAPostRow: View {
    
    let loc: String = "Massachusetts" // <--- initialized
    var post: PostModel   // <-- not initialized
    @ObservedObject var MApostData : MAPostViewModel  // <-- not initialized
    let uid = Auth.auth().currentUser!.uid  // <-- avoid doing this "!"
    
    init(post: PostModel, MApostData: MAPostViewModel) {  // <-- need to do this
        self.post = post  // <--- now initialized
        self.MApostData = MApostData // <--- now initialized
        
        UINavigationBar.appearance().barTintColor = UIColor.clear
        UINavigationBar.appearance().tintColor = .clear
        UINavigationBar.appearance().isOpaque = true   
    }
  • It didnt work when I clicked on the transition to the viewcontroller the navigationbar was just black and took away the back button. – Bstew Jun 26 '21 at 16:33