首次使用CloudKit Records创buildNSCache – closures?

我有一个项目,我即将完成。 当我将CloudKitlogging下载到数组中以显示在桌面视图中时,出现了最后一个问题。 这是我的控制器查询部分的代码。

for result in results! { let tablerestaurant = Restaurant() if let name = result.value(forKey: "Name") as! String? { tablerestaurant.name = name } // Do same for image if let imageAsset = result.object(forKey: "Picture") as! CKAsset? { if let data = try? Data(contentsOf: imageAsset.fileURL) { tablerestaurant.image = UIImage(data: data) } } self.tablerestaurantarray.append(tablerestaurant) // tablerestaurant is an array holding string and image instances of class Restaurant self.restaurantArray.append(result) // restaurantArray holds CKRecords to be segued to the next controller OperationQueue.main.addOperation( { () -> Void in self.tableView.reloadData() self.activity.isHidden = true })} 

这里是我的cellForRowAtIndexPath Tablecell函数,caching部分与全局数组一起在这里注释

  var tablerestaurantarray: [Restaurant] = [] let cache = NSCache<NSString, Restaurant>() 

 override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { let cell = tableView.dequeueReusableCell(withIdentifier: "restaurantcell") as? RestaurantTableCell /////////this is the cache portion////////////// var oneRestaurant: Restaurant = tablerestaurantarray[indexPath.row] if let cachedVersion = cache.Restaurant(forKey: "image") { oneRestaurant = cachedVersion } else { oneRestaurant = Restaurant() cache.setObject(oneRestaurant, forKey: "image") } ////////////////////////// let restaurant: Restaurant = tablerestaurantarray[indexPath.row] cell?.name?.text = oneRestaurant.name // cell?.picture?.image = oneRestaurant.image return cell! 

我试图从这里https://www.hackingwithswift.com/example-code/system/how-to-cache-data-using-nscache复制这个简单的caching过程,并将其转录为我自己的使用。 但是,debugging器指出

 NSCache<NSString, Restaurant> has no member Restaurant 

它做的。 所以我失去了我的问题。 你有想法吗?

更新2

  var tablerestaurantarray: [Restaurant] = [] let cache = NSCache<NSString , Restaurant>() var oneRestaurant: Restaurant = tablerestaurantarray[indexPath.row] if let cachedVersion = cache.object(forKey: "image") { } //error is called here { oneRestaurant = cachedVersion } else { oneRestaurant = Restaurant() cache.setObject(oneRestaurant, forKey: "image") }