我一直在尋找這個答案的天。每當我嘗試實例化一個名爲「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