[英]Class 'ProductDetailViewController' has no initializers
我正在嘗試創建可滾動的imageview。 當前使用的是Swift 4.0。 運行時,出現“沒有初始化程序”錯誤。 我不確定是什么代碼帶來了此錯誤。 我是新手,我無法追蹤正在發生的事情。
Class 'ProductDetailViewController' has no initializers
我收到此錯誤。
誰能告訴我如何解決此錯誤。
整個視圖控制器代碼如下。 提前致謝
import UIKit
import SwiftyJSON
class ProductDetailViewController: UIViewController,UIScrollViewDelegate {
//Outlers
@IBOutlet weak var scrollView: UIScrollView!
//Global variables
var productDict : JSON = []
var arrayOfProductImageUrls : Array<Any> = []
var zoomScroll : UIScrollView
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
self.initialSetup()
self.scrollViewSetup()
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
/*
// 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.
}
*/
//Mark: - Scrollview Setup
func initialSetup() {
self.scrollView.delegate = self
}
func scrollViewSetup() {
let productsArray : Array = self.arrayOfProductImageUrls;
self.scrollView.backgroundColor = UIColor.white
var frame : CGRect = CGRectMake(0, 0, self.scrollView.frame.size.width, 360)
for (index, value) in productsArray.enumerated() {
//Imageview Setup
let productImageView : UIImageView
let width : NSInteger = Int(frame.width)
productImageView.frame.origin.x = CGFloat(Int (width * index))
productImageView.frame.origin.y = 0
//Scrollview Setup
zoomScroll.frame = frame;
zoomScroll.backgroundColor = UIColor.clear
zoomScroll.showsVerticalScrollIndicator = false
zoomScroll.showsHorizontalScrollIndicator = false
zoomScroll.delegate = self
zoomScroll.minimumZoomScale = 1.0
zoomScroll.maximumZoomScale = 6.0
zoomScroll.tag = index
zoomScroll.isScrollEnabled = true
self.scrollView.addSubview(zoomScroll)
//Setting image
let imageUrl : URL = (productsArray[index] as AnyObject).url
productImageView.sd_setImage(with: imageUrl)
if index < productsArray.count {
productImageView.frame = zoomScroll.bounds
productImageView.contentMode = UIViewContentMode.redraw
productImageView.clipsToBounds = true
productImageView.backgroundColor = UIColor.clear
zoomScroll.addSubview(productImageView)
}
self.scrollView.contentSize = CGSizeMake(CGFloat(frame.origin.x) + CGFloat(width), productImageView.frame.size.height)
}
}
//Mark : Custom methods
func CGRectMake(_ x: CGFloat, _ y: CGFloat, _ width: CGFloat, _ height: CGFloat) -> CGRect {
return CGRect(x: x, y: y, width: width, height: height)
}
func CGSizeMake(_ width: CGFloat, _ height: CGFloat) -> CGSize {
return CGSize(width: width, height: height)
}
}
@Saurabh Prajapati。 不錯,趕上。
有關更多內容,Swift編程語言指出
類和結構必須在創建該類或結構的實例時將其所有存儲的屬性設置為適當的初始值。 存儲的屬性不能處於不確定狀態。
您可以在初始化程序中為存儲的屬性設置初始值,或者通過將默認屬性值分配為屬性定義的一部分來設置初始值。
讓我進一步解釋一下,問題在於沒有默認值的scrollView
屬性。 如上所述,Swift中的所有變量必須始終具有值或nil(使其為可選)。 當您將其設為可選時,默認情況下將其設置為nil,從而無需顯式為其提供值或對其進行初始化。
在這里我們正在使用!
可選,因此默認值為nil
class ViewController : UIViewController {
@IBOutlet weak var scrollView: UIScrollView!
}
將nil設置為默認值
class ViewController : UIViewController {
@IBOutlet weak var scrollView: UIScrollView! = nil
}
設置默認值,現在創建一個對象,它不是nil。
class ViewController : UIViewController {
@IBOutlet weak var scrollView: UIScrollView = UIScrollView()
}
但是我們不能那樣做,我們沒有在此處設置默認值。
class ViewController : UIViewController {
@IBOutlet weak var scrollView: UIScrollView
}
希望對您有所幫助。
我有相同的回聲,使用它很容易解決! 像var ref這樣的變量之后:DatabaseReference!
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.