我一直在寻找这个答案的天。每当我尝试实例化一个名为“Govind”的新视图控制器时,我都会遇到SIGARBT错误。我得到SIGARBT错误的原因是因为在调用的视图控制器中我使用了变量; yourVariable,ASIN,VariationImages,在我的数据库中查找特定节点。当我将它们设置为等于firebase的值时,yourVariable,ASIN和VariationImages的值不会更改。来自Firebase的值不是零。这里是我的代码斯威夫特全球变数不更新
import UIKit
import Firebase
import FirebaseDatabase
var yourVariable = ""
var ProductsNumber = 100
var ASIN = ""
var Weblink = ""
var VariationImages = 5
class Initial: UIViewController, UICollectionViewDataSource,
UICollectionViewDelegate {
@IBOutlet weak var FrontPageCollectionView: UICollectionView!
var UIFrame = UIScreen.main.bounds
var ref: DatabaseReference!
var DatabaseHandle = nil as DatabaseHandle!
override func viewDidLoad() {
super.viewDidLoad()
ref = Database.database().reference()
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
return 3
}
func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
self.DatabaseHandle = ref.child("Frontpage").child(String(indexPath.row)).observe(.value, with: { (TheCategory) in
yourVariable = TheCategory.childSnapshot(forPath: "Category").value as! String
ASIN = TheCategory.childSnapshot(forPath: "ASIN").value as! String
self.DatabaseHandle = self.ref.child(TheCategory.childSnapshot(forPath: "Category").value as! String).child(TheCategory.childSnapshot(forPath: "ASIN").value as! String).child("VariationImages").observe(.value, with: { (NumberOfVariationImages) in
VariationImages = Int(NumberOfVariationImages.childrenCount)
})
})
CallGovind()
}
func CallGovind() {
let storyboard = UIStoryboard(name: "Main", bundle: nil)
let controller = storyboard.instantiateViewController(withIdentifier: "Govind")
controller.modalPresentationStyle = .popover
self.present(controller, animated: true, completion: nil)
}
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell
{
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "FrontpageCell", for: indexPath) as! myCell
self.DatabaseHandle = ref.child("Frontpage").child(String(indexPath.row)).child("Thumbnail").observe(.value, with: { (snapshot) in
cell.FrontpageImages.sd_setImage(with: URL(string: snapshot.value as! String), placeholderImage: #imageLiteral(resourceName: "Menu"), options: [.continueInBackground, .progressiveDownload])
})
cell.FrontpageImages.contentMode = .scaleAspectFill
cell.FrontpageImages.layer.cornerRadius = 5
cell.FrontpageImages.clipsToBounds = true
return cell
}
}
/*
// 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.
}
*/
}
//Here's where the data goes into the second view controller
import UIKit
import Firebase
import FirebaseDatabase
class Testing: UIViewController, UICollectionViewDataSource, UICollectionViewDelegate {
var ref: DatabaseReference!
var DatabaseHandle = nil as DatabaseHandle!
override func viewDidLoad() {
super.viewDidLoad()
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
//Populate view
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell
{
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "cells", for: indexPath) as! myCell
self.DatabaseHandle = ref.child(yourVariable).child(ASIN).child("VariationImages").child(String(indexPath.row)).observe(.value, with: { (snapshot) in
cell.myImageViews.sd_setImage(with: URL(string: snapshot.value as! String), placeholderImage: #imageLiteral(resourceName: "Menu"), options: [.continueInBackground, .progressiveDownload])
})
return cell
}
无处不在DataBaseHandle是,产生一个错误,因为它是一个空字符串,因为变量没有更新
请显示崩溃发生的代码。 – Paulw11
我编辑了帖子并添加了您请求的代码,感谢您回复@ Paulw11 –
您可以发布Govind的代码吗?由于这是崩溃,你应该发布它。 – ryantxr