Creating RestaurantPhotoItem

The process for creating RestaurantPhotoItem is similar to creating ReviewItem, and you will do so now. Perform the following steps:

  1. Right-click the LetsEat folder and choose New Group. Name the group PhotoReviews.
  2. Right-click the PhotoReviews folder and select New File.
  3. iOS should already be selected. Choose Swift File and then click Next.
  4. Name this file RestaurantPhotoItem. Click Create.
  5. Modify the file as shown:
import UIKit

struct RestaurantPhotoItem {
var photo:UIImage?
var date:Date?
var restaurantID:Int?
var uuid = UUID().uuidString

var photoData:NSData {
guard let image = photo else {
return NSData()
}
return NSData(data: image.pngData()!)
}
}

extension RestaurantPhotoItem {
init(data:RestaurantPhoto) {
self.restaurantID = Int(data.restaurantID)
if let restaurantPhoto = data.photo {
self.photo = UIImage(data:restaurantPhoto,
scale:1.0)}
if let uuid = data.uuid { self.uuid = uuid }
if let reviewDate = data.date { self.date =
reviewDate }
}
}

Similar to ReviewItem, the properties of the RestaurantPhotoItem structure are the same as the RestaurantPhoto entity's attributes. There is one additional computed property, photoData, which is used to store the representation of photo in binary data format.

The initializer creates a RestaurantPhotoItem instance and maps the attributes from the RestaurantPhoto entity to properties in RestaurantPhotoItem instance. Note the conversion from binary data to UIImage when setting the value for photo.

Now that you have ReviewItem and RestaurantPhotoItem, let's set up the Core Data manager, which will be used to set up the Core Data components.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.144.90.182