Firebase Element May not exist on a call, iOS, Swift -


i using firebase database , storage hold images , descriptions. @ moment have coded have 8 images , 8 descriptions. trying work out how if user selects4 pictures , puts in 4 descriptions allows called back. works fine 8 when add 4 , select table called crashes error 5th photo var doesn't hold anything.

below code using call data table , crashes.

func configurecell(post: post) {     self.post = post     self.carimages = []      if post.imageurl1 != "" {         self.carimages.append(post.imageurl1)     }     if post.imageurl2 != ""{         self.carimages.append(post.imageurl2)     }     if post.imageurl3 != "" {         self.carimages.append(post.imageurl3)     }     if post.imageurl4 != "" {         self.carimages.append(post.imageurl4)     }     if post.imageurl5 != "" {         print ("tony: \(post.imageurl5)")         self.carimages.append(post.imageurl5)     }     if post.imageurl6 != "" {         self.carimages.append(post.imageurl6)     }     if post.imageurl7 != "" {         self.carimages.append(post.imageurl7)     }     if post.imageurl8 != "" {         self.carimages.append(post.imageurl8)     } 

and crash happens on imageurl5

import foundation import firebase  class post {     private var _imageurl1: string! private var _imageurl2: string! private var _imageurl3: string! private var _imageurl4: string! private var _imageurl5: string! private var _imageurl6: string! private var _imageurl7: string! private var _imageurl8: string!  private var _postkey: string! private var _postref: databasereference! private var _photoinfo1: string! private var _photoinfo2: string! private var _photoinfo3: string! private var _photoinfo4: string! private var _photoinfo5: string! private var _photoinfo6: string! private var _photoinfo7: string! private var _photoinfo8: string!     var profileimageurl: string {     return _profileimageurl }  var imageurl1: string {     return _imageurl1 } var imageurl2: string {     return _imageurl2 } var imageurl3: string {     return _imageurl3 } var imageurl4: string {     return _imageurl4 } var imageurl5: string {     return _imageurl5 } var imageurl6: string {     return _imageurl6 }    var imageurl7: string {     return _imageurl7 }    var imageurl8: string {     return _imageurl8 }   var postkey: string {     return _postkey }    var photoinfo1: string {     return _photoinfo1 } var photoinfo2: string {     return _photoinfo2 } var photoinfo3: string {     return _photoinfo3 } var photoinfo4: string {     return _photoinfo4 } var photoinfo5: string {     return _photoinfo5 } var photoinfo6: string {     return _photoinfo6 } var photoinfo7: string {     return _photoinfo7 } var photoinfo8: string {     return _photoinfo8 }  init(imageurl1: string, imageurl2: string, imageurl3: string, imageurl4: string, imageurl5: string, imageurl6: string, imageurl7: string, imageurl8: string, photoinfo1: string, photoinfo2: string, photoinfo3: string, photoinfo4: string, photoinfo5: string, photoinfo6: string, photoinfo7: string, photoinfo8: string) {       self._imageurl1 = imageurl1     self._imageurl2 = imageurl2     self._imageurl3 = imageurl3     self._imageurl4 = imageurl4     self._imageurl5 = imageurl5     self._imageurl6 = imageurl6     self._imageurl7 = imageurl7     self._imageurl8 = imageurl8      self._photoinfo1 = photoinfo1     self._photoinfo2 = photoinfo2     self._photoinfo3 = photoinfo3     self._photoinfo4 = photoinfo4     self._photoinfo5 = photoinfo5     self._photoinfo6 = photoinfo6     self._photoinfo7 = photoinfo7     self._photoinfo8 = photoinfo8  }  init(postkey: string, postdata: dictionary<string, anyobject>) {     self._postkey = postkey        if let imageurl1 = postdata["imageurl1"] as? string {         self._imageurl1 = imageurl1     }     if let imageurl2 = postdata["imageurl2"] as? string {         self._imageurl2 = imageurl2     }     if let imageurl3 = postdata["imageurl3"] as? string {         self._imageurl3 = imageurl3     }     if let imageurl4 = postdata["imageurl4"] as? string {         self._imageurl4 = imageurl4     }     if let imageurl5 = postdata["imageurl5"] as? string {         self._imageurl5 = imageurl5     }     if let imageurl6 = postdata["imageurl6"] as? string {         self._imageurl6 = imageurl6     }     if let imageurl7 = postdata["imageurl7"] as? string {         self._imageurl7 = imageurl7     }     if let imageurl8 = postdata["imageurl8"] as? string {         self._imageurl8 = imageurl8     }       if let photoinfo1 = postdata["photoinfo1"] as? string {         self._photoinfo1 = photoinfo1     }     if let photoinfo2 = postdata["photoinfo2"] as? string {         self._photoinfo2 = photoinfo2     }     if let photoinfo3 = postdata["photoinfo3"] as? string {         self._photoinfo3 = photoinfo3     }     if let photoinfo4 = postdata["photoinfo4"] as? string {         self._photoinfo4 = photoinfo4     }     if let photoinfo5 = postdata["photoinfo5"] as? string {         self._photoinfo5 = photoinfo5     }     if let photoinfo6 = postdata["photoinfo6"] as? string {         self._photoinfo6 = photoinfo6     }     if let photoinfo7 = postdata["photoinfo7"] as? string {         self._photoinfo7 = photoinfo7     }     if let photoinfo8 = postdata["photoinfo8"] as? string {         self._photoinfo8 = photoinfo8     }      _postref = dataservice.ds.ref_posts.child(_postkey)  } } 

crash @ 5th image

firebase data

perhaps answer via example

we have post class private var that's optional - meaning contain value or nil. indicated string?

when external imageurl1 accessed may return value or may nil, again ? indicates that.

class post{     private var _imageurl1: string?      var imageurl1: string? {         return _imageurl1     }      init(url: string?) {  //this nil!         self._imageurl1 = url     } }  let apost = post(url: "testurl")  if apost.imageurl1 != nil {     print(apost.imageurl1!) }  //or preferred way unwraps optional automatically if let aposturl = apost.imageurl1 {     print(aposturl) } else {     print("it nil") }  //here's nil example let bpost = post(url: nil) if let bposturl = bpost.imageurl1 {     print(bposturl) } else {     print("it nil")  //prints nil } 

Comments

Popular posts from this blog

node.js - Node js - Trying to send POST request, but it is not loading javascript content -

javascript - Replicate keyboard event with html button -

javascript - Web audio api 5.1 surround example not working in firefox -