title: swift data not transferred from UIBarButtonItem to another viewcontroller
body: <p>This is an extension of an earlier question:-  <a href="https://stackoverflow.com/q/63805844/7444874">ios Swift Items do not get added to cart</a></p>
  <p>The original issue is resolved. Now I have a supplementary hitch:-</p>
  <p>The issue is in CartViewController - When I click on &quot;Checkout(2)&quot; rightBarButtonItem in the ProductViewController, it shows error in CartviewController's &quot;numberOfRowsInSection&quot; function - Kindly see image &amp; codes below:-</p>
  <p>class ProductViewController -</p>
  <pre><code>  import UIKit
    
    class ProductViewController: UIViewController, UITableViewDataSource,   UITableViewDelegate {
  let sections = [&quot;Section A&quot;, &quot;Section B&quot;]
  let rowspersection = [3,1]
  fileprivate var cart = Cart()
  
   @IBOutlet weak var tableView: UITableView!
   override func viewDidLoad() {
      super.viewDidLoad()
      tableView.delegate = self
      tableView.dataSource = self    
      }
  
  override func viewWillAppear(_ animated: Bool) {
      super.viewWillAppear(animated)
      
      //Workaround to avoid the fadout the right bar button item
      self.navigationItem.rightBarButtonItem?.isEnabled = false
      self.navigationItem.rightBarButtonItem?.isEnabled = true
      
      //Update cart if some items quantity is equal to 0 and reload the product table   and right button bar item
      
      cart.updateCart()
     self.navigationItem.rightBarButtonItem?.title = &quot;Checkout (\(cart.items.count))&quot;
      tableView.reloadData()
   }
  
   override func didReceiveMemoryWarning() {
      super.didReceiveMemoryWarning()
      // Dispose of any resources that can be recreated.
  }
   override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
   if segue.identifier == &quot;showCart&quot; {
   if let cartViewController = segue.destination as? CartViewController {
   cartViewController.cart = self.cart
       }
     }
   }
  
  func numberOfSections(in tableView: UITableView) -&gt; Int {
      return sections.count
   }
  func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -&gt; Int {
      return rowspersection[section]
   }
  func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -&gt; UITableViewCell   {
      let cell = tableView.dequeueReusableCell(withIdentifier: &quot;ProductTableViewCell&quot;) as!   ProductTableViewCell
      cell.delegate = self // original issue was here, now resolved.
     
      var index = indexPath.row
      if indexPath.section != 0, rowspersection.count &gt; indexPath.section - 1{
          index += rowspersection[indexPath.section - 1]
      }
      
    if index &lt; productarray.count{
          let data = productarray[index]
       cell.name?.text = data.name
          cell.imageView?.image =  data.imagename
    }
       let product = productarray[indexPath.item]
       cell.setButton(state: self.cart.contains(product: product))
      return cell
     }
  func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -&gt; CGFloat {
      return 44
    }
  
  func tableView(_ tableView: UITableView, titleForHeaderInSection section: Int) -&gt; String? {
      switch(section) {
      case 0:return &quot;Section A&quot;
      case 1:return &quot;Section B&quot;
      default :return &quot;&quot;
          
        }
      }
    }
  
   extension ProductViewController: CartDelegate {
  
  // MARK: - CartDelegate
  func updateCart(cell: ProductTableViewCell) {
      guard let indexPath = tableView.indexPath(for: cell) else { return }
      let product = productarray[indexPath.item]
      
      //Update Cart with product
      cart.updateCart(with: product)
      self.navigationItem.rightBarButtonItem?.title = &quot;Checkout (\(cart.items.count))&quot;
     }
   }
  </code></pre>
  <p><a href="https://i.stack.imgur.com/0mt8q.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/0mt8q.png" alt="enter image description here" /></a></p>
  <p>The issue is in CartViewController - When I click on &quot;Checkout(2)&quot; rightBarButtonItem in the ProductViewController(see image above), it shows error in CartviewController's &quot;numberOfRowsInSection&quot; function -  see CartViewController code below:-</p>
  <pre><code> import UIKit
   
   class CartViewController: UIViewController {
   @IBOutlet weak var tableView: UITableView!
   @IBOutlet weak var totalView: UIView!
   @IBOutlet weak var totalLabel: UILabel!
  
   var cart: Cart? = nil
  fileprivate let reuseIdentifier = &quot;CartItemCell&quot;
  override func viewDidLoad() {
      super.viewDidLoad()
      tableView.tableFooterView = UIView(frame: .zero)
    }
   }
  
  extension CartViewController: UITableViewDelegate, UITableViewDataSource {
  // MARK: - Table view data source
  func numberOfSections(in tableView: UITableView) -&gt; Int {
      // #warning Incomplete implementation, return the number of sections
      return 1
  }
  func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -&gt; Int {
      // #warning Incomplete implementation, return the number of rows
      return (cart?.items.count)!  /*Error - Thread 1: EXC_BAD_INSTRUCTION (code=EXC_I386_INVOP, subcode=0x0)*/
  }
  
  func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -&gt; UITableViewCell {
    let cell = tableView.dequeueReusableCell(withIdentifier: reuseIdentifier, for: indexPath) as! CartItemTableViewCell
      
   if let cartItem = cart?.items[indexPath.item] {
   cell.delegate = self as CartItemDelegate
          
        //  cell.nameLabel.text = cartItem.product.name
       //   cell.priceLabel.text = cartItem.product.price
          cell.quantityLabel.text = String(describing: cartItem.quantity)
          
        cell.quantity = cartItem.quantity
          // cell.contentView.backgroundColor = !cell.decrementButton.isEnabled ? .white : .blue
      }
      return cell
    }
  }
  
  
   extension CartViewController: CartItemDelegate {
  
  // MARK: - CartItemDelegate
  func updateCartItem(cell: CartItemTableViewCell, quantity: Int) {
      guard let indexPath = tableView.indexPath(for: cell) else { return }
      guard let cartItem = cart?.items[indexPath.row] else { return }
      
      //Update cart item quantity
      cartItem.quantity = quantity
      
      //Update displayed cart total
   //   guard let total = cart?.total else { return }
      //totalLabel.text = String(total)
      //   print(total)
      }
    }
  </code></pre>
  <p>My Models -</p>
  <p>struct Product -</p>
  <pre><code> import UIKit
   
   struct Product:Equatable {
   let name : String
   var quantity : Int
   var price : Double
   let imagename: UIImage
     // var subTotal : Double {
      //return Double(quantity) * price }
   }
   var productarray = [Product(name: &quot;a&quot;, quantity: 5, price: 5.0,imagename:#imageLiteral(resourceName: &quot;CakeImage&quot;)),
    Product(name: &quot;b&quot;, quantity: 10, price: 10.0, imagename:#imageLiteral(resourceName: &quot;PeasImge&quot;)),Product(name: &quot;a&quot;, quantity: 5, price: 5.0,imagename:#imageLiteral(resourceName: &quot;vectorlogo&quot;)),
                  Product(name: &quot;b&quot;, quantity: 10, price: 10.0, imagename:#imageLiteral(resourceName: &quot;blue&quot;)),]
  </code></pre>
  <p>class CartItem -</p>
  <pre><code>import Foundation
  
  class CartItem {
  var quantity : Int = 1
  var product : Product
  // var subTotal : Float { get { return Float(product.price) * Float(quantity) } }
  init(product: Product) {
      self.product = product
    }
  }
  </code></pre>
  <p>class Cart -</p>
  <pre><code>  import Foundation
  
    class Cart {
    var items : [CartItem] = []
    }
  
  extension Cart {
  
     /* var total: Float {
      get { return items.reduce(0.0) { value, item in
          value + item.subTotal
          }
      }
   }*/
  
    var totalQuantity : Int {
      get { return items.reduce(0) { value, item in
          value + item.quantity
          }
      }
   }
  func updateCart(with product: Product) {
      if !self.contains(product: product) {
          self.add(product: product)
      } else {
          self.remove(product: product)
      }
  }
  func updateCart() {
      
      for item in self.items {
          if item.quantity == 0 {
              updateCart(with: item.product)
          }
      }
  }
  
  func add(product: Product) {
      let item = items.filter { $0.product == product }
      
      if item.first != nil {
          item.first!.quantity += 1
      } else {
          items.append(CartItem(product: product))
      }
  }
  
  func remove(product: Product) {
      guard let index = items.firstIndex(where: { $0.product == product }) else { return}
      items.remove(at: index)
  }
  
  
  func contains(product: Product) -&gt; Bool {
      let item = items.filter { $0.product == product }
      return item.first != nil
     }
   }
  </code></pre>
  <p>--</p>
  <p>The UITableViewCells -</p>
  <p>class ProductTableViewCell -</p>
  <pre><code> import UIKit
   protocol CartDelegate {
   func updateCart(cell: ProductTableViewCell) }
   class ProductTableViewCell: UITableViewCell {
   weak var myParent:ProductViewController?
   @IBOutlet weak var name: UILabel!
   @IBOutlet weak var price: UILabel!
   @IBOutlet weak var imagename: UIImageView!
   @IBOutlet weak var addToCartButton: UIButton!
  
   var delegate: CartDelegate?
  
   override func awakeFromNib() {
      super.awakeFromNib()
      // Initialization code
      
     addToCartButton.layer.cornerRadius = 5
    addToCartButton.clipsToBounds = true
   }
  
    func setButton(state: Bool) {
      addToCartButton.isSelected = state
      addToCartButton.backgroundColor = (!addToCartButton.isSelected) ? .black : .red
  }
  
     @IBAction func addToCart(_ sender: Any) {
      setButton(state: !addToCartButton.isSelected)
      self.delegate?.updateCart(cell: self)
      }
    }
  </code></pre>
  <p>class CartItemTableViewCell-</p>
  <pre><code> import UIKit
   
   protocol CartItemDelegate {
   func updateCartItem(cell: CartItemTableViewCell, quantity: Int)
   }
   class CartItemTableViewCell: UITableViewCell {
  
  @IBOutlet weak var nameLabel: UILabel!
  @IBOutlet weak var priceLabel: UILabel!
  
  @IBOutlet weak var incrementButton: UIButton!
  @IBOutlet weak var decrementButton: UIButton!
  @IBOutlet weak var quantityLabel: UILabel!
  
  var delegate: CartItemDelegate?
  var quantity: Int = 1
  
  override func awakeFromNib() {
      super.awakeFromNib()
      // Initialization code
      
      incrementButton.layer.cornerRadius = 10
      incrementButton.clipsToBounds = true
      
      decrementButton.layer.cornerRadius = 10
      decrementButton.clipsToBounds = true
   }
  
  override func setSelected(_ selected: Bool, animated: Bool) {
      super.setSelected(selected, animated: animated)
   }
  
  @IBAction func updateCartItemQuantity(_ sender: Any) {
      if (sender as! UIButton).tag == 0 {
          quantity = quantity + 1
      } else if quantity &gt; 0 {
          quantity = quantity - 1
      }
      
      decrementButton.isEnabled = quantity &gt; 0
      decrementButton.backgroundColor = !decrementButton.isEnabled ? .gray : .black
      
      self.quantityLabel.text = String(describing: quantity)
      self.delegate?.updateCartItem(cell: self, quantity: quantity)
     }
   }
  </code></pre>
  <p>The segues are connected properly. So, I suspect  rightBarButtonItem i.e. Checkout(2) to another CartViewcontroller.</p>
  <p>I have been thinking over it for quite some time.</p>
  <p>I would sincerely appreciate your assistance. It would mean a lot to me.</p>