6.7. Getting the expense back to ViewController
We now need to get the expense back when we press buttonAdd. We need to tweak the data model Expense to accommodate the image, right?
Update Expense data model:
Let's open the Expense.swift file and update the file as the following:
//
// Expense.swift
// App6
//
// Created by Sakib Miazi on 5/18/23.
//
import Foundation
import UIKit
struct Expense{
var title: String?
var amount: Double?
var type: String?
var image: UIImage?
init(title: String, amount: Double, type: String, image: UIImage) {
self.title = title
self.amount = amount
self.type = type
self.image = image
}
}Update AddExpenseViewController's onAddButtonTapped() method:
We write:
Here, we are creating a new expense with the title, amount, type, and image. The pickedImage might be nil since it is Optional. So if it becomes nil (at any point), we send a default system image named "photo" to be safe.
Update the TableView to display the image in the cell
Open ViewController.swift. We need to update where we are adopting the TableView protocools.
We unwrap the image, and then set it to cell.imageReceipt.image.
Finally, let's run the app!

Last updated
Was this helpful?