title: Swift Accessing Data Structure Inside Cell Setup
body: <p>I am appending a Firebase Data snapshot to a NSObject of items being a "customer", "employee", and "business". Set up like this:</p>
  
  <pre><code>var customerData = [CustomerData]()
  var employeeData = [EmployeeData]()
  var businessData = [BusinessData]()
  
  func getCustomerData() {
      Database.database().reference().child("user_profiles").observe(.childAdded, with: { snapshot in
          self.customerData.append(CustomerData(snapshot: snapshot))
      })
  }
  
  func getEmployeeData() {
      Database.database().reference().child("employees").observe(.childAdded, with: { snapshot in
          self.employeeData.append(EmployeeData(snapshot: snapshot))
      })
  }
  
  func getBusinessData() {
      Database.database().reference().child("Businesses").observe(.childAdded, with: { snapshot in
          self.businessData.append(BusinessData(snapshot: snapshot))
      })
  }
  </code></pre>
  
  <p><strong>Data structure is the same for customer, employees, and business as below</strong></p>
  
  <pre><code>import UIKit
  import Firebase
  
  class CustomerData: NSObject {
  
  var customerName: String?
  var customerPicture: String?
  var customerUID: String?
  
  init(snapshot: DataSnapshot) {
      if let dictionary = snapshot.value as? [String: AnyObject] {
          customerName = dictionary["name"] as? String
          customerUID = dictionary["uid"] as? String
          customerPicture = dictionary["profPicString"] as? String
      }
  }
  }
  </code></pre>
  
  <p>I just want to access this snapshot data inside the cell to keep my message details up-to-date, like the profile picture and name. Below is my cell set up:</p>
  
  <pre><code> override func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -&gt; UICollectionViewCell {
      let cell = collectionView.dequeueReusableCell(withReuseIdentifier: cellId, for: indexPath) as! ChatMessageCell
  
      cell.chatLogController = self
  
      let customer = customerData
      let employee = employeeData
      let business = businessData
  
      let message = messages[indexPath.row]
  
      cell.message = message
      cell.customer = customer
      cell.employee = employee
      cell.business = business
  
      setupChatMessageCell(cell,message,customer,employee,business)
  
      if let text = message.text {
          cell.textView.text = text
          cell.bubbleWidthAnchor?.constant = estimateSizeOfText(text).width + 32
          cell.textView.isHidden = false
      } else if message.imageUrl != nil {
          cell.bubbleWidthAnchor?.constant = 200
          cell.textView.isHidden = true
      }
  
      cell.playButton.isHidden = message.videoUrl == nil
  
      return cell
  }
  
      private func setupChatMessageCell(_ cell: ChatMessageCell, _ message: GroupMessage, _ customer: CustomerData, _ employee: EmployeeData, _ business: BusinessData) {
  
  
      if message.fromId == customer.customerUID {
          //outgoing messages
          cell.bubbleView.backgroundColor = ChatMessageCell.blueColor
          cell.textView.textColor = .white
          cell.bubbleLeftAnchor?.isActive = false
          cell.bubbleRightAnchor?.isActive = true
          cell.profileImageView.isHidden = true
          cell.nameLabel.textColor = .gray
          cell.nameRightAnchor?.isActive = true
          cell.nameLeftAnchor?.isActive = false
          cell.nameLabel.text = message.name?.description
          //cell.nameLabel.text = message.customerName
      } else if message.fromId == employee.employeeUID {
          //incoming messagese
          let customerImage = employee.employeePicture
          cell.profileImageView.loadImageUsingCacheWithUrlString(customerImage!)
          cell.profileImageView.isHidden = false
          cell.bubbleView.backgroundColor = UIColor(red: 240, green: 240, blue: 240)
          cell.textView.textColor = .black
          cell.bubbleLeftAnchor?.isActive = true
          cell.bubbleRightAnchor?.isActive = false
          cell.profileImageView.isHidden = false
          cell.nameRightAnchor?.isActive = false
          cell.nameLeftAnchor?.isActive = true
          cell.nameLabel.textColor = .black
          cell.nameLabel.text = message.name?.description
      } else if message.fromId == business.businessUID {
          let customerImage = business.businessPicture
          cell.profileImageView.loadImageUsingCacheWithUrlString(customerImage!)
          cell.profileImageView.isHidden = false
          cell.bubbleView.backgroundColor = UIColor(red: 240, green: 240, blue: 240)
          cell.textView.textColor = .black
          cell.bubbleLeftAnchor?.isActive = true
          cell.bubbleRightAnchor?.isActive = false
          cell.profileImageView.isHidden = false
          cell.nameRightAnchor?.isActive = false
          cell.nameLeftAnchor?.isActive = true
          cell.nameLabel.textColor = .black
          cell.nameLabel.text = message.name?.description
      }
  
      if let imageUrl = message.imageUrl {
          cell.messageImageView.loadImageUsingCacheWithUrlString(imageUrl)
          cell.messageImageView.isHidden = false
          cell.bubbleView.backgroundColor = .clear
      } else {
          cell.messageImageView.isHidden = true
      }
  }
  </code></pre>
  
  <p>The problem is that I don't think accessing it as a "[index.path]" is the correct way just how I am doing with "messages". How can I access these data structures within the cell setup so I can keep my users information always up-to-date? I am getting errors like "Cannot assign value of type '[CustomerData]' to type 'CustomerData?'" so what is the proper way to access these data structures inside the cell?</p>