我得到了这个例外:
由于未捕获的异常而终止应用程序&#39; NSUnknownKeyException&#39;,原因:&#39; [setValue:forUndefinedKey:]:此类与关键ProductsCollection不符合键值编码。&#39; < / p>
我阅读了之前有关此内容的帖子,并且我检查了两次 - 我的UIVewCollection
(ProductsCollection
)与该商店相关联。
我的班级:
class SellProductsView: ProductsCollectionViewController
{
override func viewDidLoad()
{
// Do any additional setup after loading the view.
self.ProductsCollection.delegate = self
self.ProductsCollection.dataSource = self;
LoadProducts(productsToSellOrBuy: "ToSell")
super.viewDidLoad()
}
override func prepare(for segue: UIStoryboardSegue, sender: Any?)
{
if segue.identifier == "view_product_information"
{
let prodPageView = segue.destination as! SellProductPageView
PrepareProductForSegue(prodPageView: prodPageView)
}
}
}
我的基类:
class ProductsCollectionViewController: UIViewController, UICollectionViewDelegate, UICollectionViewDataSource
{
@IBOutlet weak var ProductsCollection: UICollectionView!
var ref: DatabaseReference?
var databaseHandle: DatabaseHandle?
@IBAction func unwindFromProductPageView(segue: UIStoryboardSegue)
{
}
var products = [Product]()
override func viewDidLoad()
{
// Do any additional setup after loading the view.
self.ProductsCollection.delegate = self
self.ProductsCollection.dataSource = self;
super.viewDidLoad()
}
internal func LoadProducts(productsToSellOrBuy: String)
{
if productsToSellOrBuy != "ToBuy" && productsToSellOrBuy != "ToSell"
{
// Throw error
}
// Set firebase reference
ref = Database.database().reference()
let loggedOnUserID = Auth.auth().currentUser?.uid
// Retrieve the products and listen for changes
databaseHandle = ref?.child("Products").observe(.childAdded, with:
{ (snapshot) in
// Code to execute when new product is added
let prodValue = snapshot.value as? NSDictionary
let prodToLoad = prodValue?[productsToSellOrBuy] as? Bool // Checks if this is to sell or buy
if loggedOnUserID != prodValue?["Owner"] as? String, prodToLoad == true
{
let prodName = prodValue?["Name"] as? String ?? ""
let prodPrice = prodValue?["Price"] as? Double ?? -1
let prodDesc = prodValue?["Description"] as? String ?? ""
let prodURLS = prodValue?["MainImage"] as? String
let prodAmount = prodValue?["Amount"] as? Int ?? 0
let prodID = snapshot.key
let prodToAddToView = Product(name: prodName, price: prodPrice, currency: "NIS", description: prodDesc, location: "IL",
toSell: false, toBuy: false, owner: "No one", uniqueID: prodID, amount: prodAmount, mainImageURL: prodURLS)
self.products.append(prodToAddToView)
DispatchQueue.main.async {
self.ProductsCollection.reloadData()
}
}
})
}
override func didReceiveMemoryWarning()
{
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int
{
return products.count
}
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell
{
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "product_collection_cell", for: indexPath) as! ProductsCollectionViewCell
//cell.ProductName.text
let requestListenRef = ref?.child("Products").child(products[indexPath.row].UniqueID()).child("MainImage")
requestListenRef?.observe(DataEventType.value, with:
{
(snapshot) in
let mainImage = snapshot.value as? String
if mainImage == nil
{
// No main image exists
cell.ProductImageView.image = UIImage(named: "DefaultProductImage")
}
else
{
// Main Image exists
let url = URL(string: mainImage!)
if let data = try? Data(contentsOf: url!)
{
cell.ProductImageView.image = UIImage(data: data)
}
}
})
// Set fields
let prodInCell = products[indexPath.row]
cell.ProductName.text = prodInCell.Name()
cell.ProductPrice.text = String(prodInCell.Price())
cell.productUniqueID = prodInCell.UniqueID()
return cell
}
func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath)
{
// Display selected Item
prodToLoad = products[indexPath.row]
performSegue(withIdentifier: "view_product_information", sender:self )
}
var prodToLoad: Product?
override func prepare(for segue: UIStoryboardSegue, sender: Any?)
{
if segue.identifier == "view_product_information"
{
let prodPageView = segue.destination as! ProductPageView
PrepareProductForSegue(prodPageView: prodPageView)
}
}
internal func PrepareProductForSegue(prodPageView: ProductPageView)
{
prodPageView.productToDisplay = prodToLoad
prodPageView.pictures = [UIImageView] ()
if (prodToLoad?.Images().count != 0)
{
let mainImage = prodToLoad?.GetMainImageURLString()
// Main Image exists
let url = URL(string: mainImage!)
if let data = try? Data(contentsOf: url!)
{
let imageView = UIImageView()
imageView.image = UIImage(data: data)
prodPageView.pictures?.append(imageView)
}
}
else
{
let imageView = UIImageView()
imageView.image = #imageLiteral(resourceName: "DefaultProductImage")
prodPageView.pictures?.append(imageView)
}
}
/*
// MARK: - Navigation
// In a storyboard-based application, you will often want to do a little preparation before navigation
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
// Get the new view controller using segue.destinationViewController.
// Pass the selected object to the new view controller.
}
*/
}
另外:
当我删除连接时,应用程序不会崩溃。一旦我重新连接,它再次崩溃。 ProductsCollection现在在两个INHERITING视图之间共享。