属性'self。*'未在super.init调用中初始化

时间:2015-04-27 08:14:59

标签: ios swift xcode6.3.1

我刚刚将我的xcode更新为6.3.1。问题是我从swift 1.2获得了这个奇怪的错误消息。我收到了这种错误信息

/Users/MNurdin/Documents/iOS/xxxxx/Library/SideBar.swift:32:15: Property 'self.originView' not initialized at super.init call

/Users/MNurdin/Documents/iOS/xxxxx/Library/SideBar.swift:38:20: Immutable value 'self.originView' may only be initialized once

关于此代码

let originView:UIView?

override init() {
        super.init() //error here

    }

    init(sourceView:UIView, menuItems:Array<String>){
        super.init() //error here
        originView = sourceView //error here

请指教。谢谢。

3 个答案:

答案 0 :(得分:21)

在任何init方法

中调用super.init之前,必须先初始化所有属性

因此,在调用super.init()

之前更改此设置
originView = sourceView //error here

例外:

  1. 可选属性
  2. 属性,默认值为
  3. lazy property

答案 1 :(得分:12)

通过

让您的using OxyPlot; using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Windows; using System.Windows.Controls; using System.Windows.Data; using System.Windows.Documents; using System.Windows.Input; using System.Windows.Media; using System.Windows.Media.Imaging; using System.Windows.Navigation; using System.Windows.Shapes; using DrawToolsLib; namespace WpfApplication18DrawProblem { /// <summary> /// Interaction logic for MainWindow.xaml /// </summary> /// public class TestData { public PlotModel PlotModel { get; set; } private OxyPlot.Series.LineSeries LS; public PlotController PlotController { get; set; } public TestData() { PlotController = new OxyPlot.PlotController(); PlotController.UnbindAll(); Random rnd = new Random(); PlotModel = new PlotModel(); LS = new OxyPlot.Series.LineSeries(); for (int i = 0; i < 10; i++) { LS.Points.Add(new DataPoint(i, rnd.NextDouble())); } PlotModel.Series.Add(LS); } public void Update() { PlotModel.InvalidatePlot(true); } } public partial class MainWindow : Window { Point currentPoint = new Point(); TestData TD = new TestData(); public MainWindow() { DataContext = TD; InitializeComponent(); } private void Canvas_MouseDown_1(object sender, System.Windows.Input.MouseButtonEventArgs e) { if (e.ButtonState == MouseButtonState.Pressed) currentPoint = e.GetPosition(this); } private void Canvas_MouseMove_1(object sender, System.Windows.Input.MouseEventArgs e) { if (e.LeftButton == MouseButtonState.Pressed) { Line line = new Line(); Ellipse Ell = new Ellipse() { Stroke = Brushes.Black, StrokeThickness = 2, }; line.Stroke = SystemColors.WindowFrameBrush; line.X1 = currentPoint.X; line.Y1 = currentPoint.Y; line.X2 = e.GetPosition(this).X; line.Y2 = e.GetPosition(this).Y; currentPoint = e.GetPosition(this); paintSurface.Children.Add(line); } TD.Update(); } } } 可以为空
originView

如果var originView: UIView?. 不可为空,则必须在调用

之前提供默认值
originView

答案 2 :(得分:1)

来自Apple的“The Swift Programming Language”一书:

“Swift的编译器执行四个有用的safety-checks以确保完成两阶段初始化而没有错误”

“指定的初始值设定项必须确保在委托一个超类初始化程序之前初始化其类引入的所有属性。”

基本上你必须先做ensure that your instance variables are in a consistent state才能做任何事情,包括调用方法。

class YourClass {
    var view: UIView
    init(view: UIView) {
        self.view = view
    }
}

在您的情况下,您可以将其设为新UIView

let originView = UIView()

或使其可以为空

let originView: UIView?

或改为使用懒惰属性

lazy var originView: UIView = {
    let view = UIView(frame: CGRect(x: 0, y: 0, width: 375, height: 200))
    // customize it
    return view
}()

使用延迟实例化时,您可以传递一个方法:

lazy var originView: UIView = self.createView()

func createView() -> UIView {
    let view = UIView(frame: CGRect(x: 0, y: 0, width: 375, height: 200))
        // customize it
        return view
}