ios - 类 'ProductDetailViewController' 没有初始化器

标签 ios iphone swift ipad

我正在尝试创建可滚动的 ImageView 。目前使用swift 4.0。运行时出现“没有初始化程序”错误。我不确定是什么代码导致了这个错误。我是 swift 的新手,我无法跟踪正在发生的事情。

类“ProductDetailViewController”没有初始值设定项我收到此错误。

谁能告诉我如何解决这个错误。

完整的 View Controller 代码如下。提前致谢

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 生主。漂亮而快速的捕捉。

有关更多信息,Swift 编程语言指出

Classes and structures must set all of their stored properties to an appropriate initial value by the time an instance of that class or structure is created. Stored properties cannot be left in an indeterminate state.

You can set an initial value for a stored property within an initializer, or by assigning a default property value as part of the property’s definition.

让我解释一下,问题出在没有默认值的 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
}

希望对您有所帮助。

关于ios - 类 'ProductDetailViewController' 没有初始化器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46580620/

相关文章:

swift - ARKit – 渲染距离超过 1000 米的物体

ios - collectionView 中的异常(_ :layout:sizeForItemAt:) when accessing layout. collectionViewContentSize

ios - 无法使用 UIBarButtonItem 显示弹出 Controller

android - 电子商务应用程序的前端技术堆栈

swift - 代码 : "Module not found" for Toast_Swift in only one view controller

swift - 在 Swift 实例中使用父类(super class)的值

ios - 从外部调用函数后,Collectionviewcontroller 上的 Swift 变量重置

ios - AS3 在按住按钮的同时连续运行代码 - Air For iOS/Android

ios - 非官方 API - 访问其他应用程序包和图标 - iOS

ios - Swift 中的流控制 - AVAudioPlayer