我正在尝试创建可滚动的imageview。目前使用的是swift 4.0。在跑步的同时我得到了#34;没有初始化者"错误。我不确定是什么代码带来了这个错误。我是swift的新手,我无法追踪发生的事情。
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)
}
}
答案 0 :(得分:2)
@Saurabh Prajapati。很好,快速捕获。
更多,Swift编程语言说明
类和结构必须将其所有存储属性设置为 适当的初始值,当该类的实例或 结构已创建。存储的属性不能留在 不确定的状态。
您可以在其中设置存储属性的初始值 初始化程序,或通过指定默认属性值作为一部分 财产的定义。
让我解释一下,问题在于--Recreating indexes
if not exists (SELECT * FROM sys.indexes WHERE name = 'IX_SearchIndex') begin
UPDATE dbo.SearchIndex SET IndexID = NEWID()
CREATE UNIQUE NONCLUSTERED INDEX [IX_SearchIndex] ON [dbo].[SearchIndex] ([IndexID])
if (SELECT count(*) from sys.fulltext_catalogs where serverproperty('isfulltextinstalled') = 1) > 0
exec sp_executesql N'CREATE FULLTEXT INDEX ON [dbo].[SearchIndex] ([Content]) KEY INDEX [IX_SearchIndex]'
end
else Begin
if (SELECT count(*) from sys.fulltext_catalogs where serverproperty('isfulltextinstalled') = 1) > 0
if not exists (SELECT * FROM sys.fulltext_indexes f
INNER JOIN sys.indexes i on f.Unique_Index_id = i.Index_id and i.object_id=f.object_id
Where f.object_id = object_id('SearchIndex') and i.name = 'IX_SearchIndex')
exec sp_executesql N'CREATE FULLTEXT INDEX ON [dbo].[SearchIndex] ([Content]) KEY INDEX [IX_SearchIndex]'
End
if not exists (SELECT * FROM sys.indexes WHERE name = 'WikiRevision_UK2') begin
UPDATE dbo.WikiRevision SET UID = NEWID()
CREATE UNIQUE NONCLUSTERED INDEX [WikiRevision_UK2] ON [dbo].[WikiRevision] ([UID])
if (SELECT count(*) from sys.fulltext_catalogs where serverproperty('isfulltextinstalled') = 1) > 0
exec sp_executesql N'CREATE FULLTEXT INDEX ON [dbo].[WikiRevision] ([PlainText]) KEY INDEX [WikiRevision_UK2]'
end
else Begin
if (SELECT count(*) from sys.fulltext_catalogs where serverproperty('isfulltextinstalled') = 1) > 0
if not exists (SELECT * FROM sys.fulltext_indexes f
INNER JOIN sys.indexes i on f.Unique_Index_id = i.Index_id and i.object_id=f.object_id
Where f.object_id = object_id('WikiRevision') and i.name = 'WikiRevision_UK2')
exec sp_executesql N'CREATE FULLTEXT INDEX ON [dbo].[WikiRevision] ([PlainText]) KEY INDEX [WikiRevision_UK2]'
End
GO
--Recreation System Procedures
if exists(select * from sys.objects where object_id = object_id('dbo.pp_EnableFullText') and objectproperty(object_id,'IsProcedure')=1)
drop proc dbo.pp_EnableFullText
go
CREATE PROCEDURE dbo.pp_EnableFullText AS
if not exists (SELECT * FROM sys.indexes WHERE name = 'IX_SearchIndex') begin
UPDATE dbo.SearchIndex SET IndexID = NEWID()
CREATE UNIQUE NONCLUSTERED INDEX [IX_SearchIndex] ON [dbo].[SearchIndex] ([IndexID])
if (SELECT count(*) from sys.fulltext_catalogs where serverproperty('isfulltextinstalled') = 1) > 0
exec sp_executesql N'CREATE FULLTEXT INDEX ON [dbo].[SearchIndex] ([Content]) KEY INDEX [IX_SearchIndex]'
end
if not exists (SELECT * FROM sys.indexes WHERE name = 'WikiRevision_UK2') begin
UPDATE dbo.WikiRevision SET UID = NEWID()
CREATE UNIQUE NONCLUSTERED INDEX [WikiRevision_UK2] ON [dbo].[WikiRevision] ([UID])
if (SELECT count(*) from sys.fulltext_catalogs where serverproperty('isfulltextinstalled') = 1) > 0
exec sp_executesql N'CREATE FULLTEXT INDEX ON [dbo].[WikiRevision] ([PlainText]) KEY INDEX [WikiRevision_UK2]'
end
GO
if exists(select * from sys.objects where object_id = object_id('dbo.pp_DisableFullText') and objectproperty(object_id,'IsProcedure')=1)
drop proc dbo.pp_DisableFullText
go
CREATE PROCEDURE dbo.pp_DisableFullText AS
if exists(SELECT * FROM sys.fulltext_indexes fi JOIN sys.indexes i ON i.[object_id] = fi.[object_id] AND i.[name] = 'IX_SearchIndex')
exec sp_executesql N'DROP FULLTEXT INDEX ON [dbo].[SearchIndex]'
if exists(select * from sys.indexes where name = 'IX_SearchIndex')
DROP INDEX [IX_SearchIndex] ON [dbo].[SearchIndex]
if exists(SELECT * FROM sys.fulltext_indexes fi JOIN sys.indexes i ON i.[object_id] = fi.[object_id] AND i.[name] = 'WikiRevision_UK2')
exec sp_executesql N'DROP FULLTEXT INDEX ON [dbo].[WikiRevision]'
if exists(select * from sys.indexes where name = 'WikiRevision_UK2')
DROP INDEX [WikiRevision_UK2] ON [dbo].[WikiRevision]
GO
属性没有默认值。如上所述,Swift中的所有变量必须始终具有值或nil(使其成为可选)。当你使它成为可选项时,你默认允许它为零,不需要显式给它一个值或初始化它。
此处我们使用scrollView
可选,因此默认值为!
nil
将nil设置为默认值
class ViewController : UIViewController {
@IBOutlet weak var scrollView: UIScrollView!
}
设置默认值,现在创建一个对象,它不是零。
class ViewController : UIViewController {
@IBOutlet weak var scrollView: UIScrollView! = nil
}
但是我们不能这样做,我们没有在这里设置默认值。
class ViewController : UIViewController {
@IBOutlet weak var scrollView: UIScrollView = UIScrollView()
}
我希望它会有所帮助。
答案 1 :(得分:0)
我有同样的回音 通过使用它很容易解决它!在像这样的任何变量之后 var ref:DatabaseReference!