简体   繁体   English

在从初始化程序返回之前,不会在所有路径上调用super init

[英]super init isn't called on all paths before returning from initializer

I made a framework that wraps Alamofire. 我制作了一个包装Alamofire的框架。

In my Framework when testing (in Test target) i have this code that works grate. 在我的框架中进行测试时(在测试目标中)我有这个代码可以工作。

import Foundation
@testable import NetworkManager

class MockRouter: Router {


enum APICalls {
    case func1
    case func2
}

var calls: APICalls!

init(calls: APICalls) {
    self.calls = calls
}

}

When i add it as a framework to a different project 当我将它作为框架添加到不同的项目时

import Foundation
import NetworkManager

class JokesRouter: Router {

enum APICalls {
    case func1
    case func2
}

var calls: APICalls!

init(calls: APICalls) {
    self.calls = calls
}


}

I get an error: 我收到一个错误:

super init isn't called on all paths before returning from initializer 在从初始化程序返回之前,不会在所有路径上调用super init

So i added Super.init() 所以我添加了Super.init()

init(calls: APICalls) {
    super.init()
    self.calls = calls
}

And now i get this error: 现在我收到这个错误:

super.init cannot be called outside of an initializer super.init不能在初始化程序之外调用

Any idea what is the problem? 不知道是什么问题?

You'll need to declare an empty public init() {} in Router . 您需要在Router声明一个空的public init() {}

Though it's a cryptic error, the problem is that the subclass cannot call super.init() because that initializer is not accessible outside of the NetworkManager module unless you declare it as public . 虽然这是一个神秘的错误,但问题是子类不能调用super.init()因为除非您将其声明为public否则无法在NetworkManager模块之外访问该初始化程序。 The automatically-generated init() in Router is not public by default. 默认情况下, Router自动生成的init()public

It worked in your test class because the @testable import sidestepped the default internal access restriction. 它在您的测试类中起作用,因为@testable import回避了默认的internal访问限制。

I was trying to create an Inheritance feature of the Swift Programming. 我试图创建Swift编程的继承功能。

I created the ViewController as super class. 我创建了ViewController作为超类。 Then I created another class as subclass name as ViewControllerA of super class ViewController 然后我创建了另一个类作为超类ViewController ViewControllerA的子类名

ViewController Class : ViewController类:

import UIKit

class ViewController: UIViewController {

  //: public variables
  var area:               CGFloat?


  override func viewDidLoad() {
      super.viewDidLoad()
      // Do any additional setup after loading the view, typically from a nib.
  }

  override func didReceiveMemoryWarning() {
      super.didReceiveMemoryWarning()
      // Dispose of any resources that can be recreated.
  }

  public func printArea(){
      print("Print area in Super class")
  }

  public func calculateArea(){

  }

}

ViewControllerA as subclass : ViewControllerA作为子类:

import UIKit

class ViewControllerA: ViewController {

    var length:                 CGFloat?


     init( len: CGFloat) {
        self.length = len
        super.init(nibName: nil, bundle: nil)
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }

    override func printArea() {
        print("The are of A \(area!)")
    }


    override func calculateArea() {
        area = length! * length!
    }
}

I have got the error. 我有错误。 Must call a designated initializer of the superclass 'ViewController' in subclass init method. Must call a designated initializer of the superclass 'ViewController'在子类init方法中Must call a designated initializer of the superclass 'ViewController'

So I declare the empty init method in super class ie ViewController 所以我在超类即ViewController声明了空的init方法

override init(nibName nibNameOrNil: String?, bundle nibBundleOrNil: Bundle?) {
    super.init(nibName: nil, bundle: nil)
}

required init?(coder aDecoder: NSCoder) {
    fatalError("init(coder:) has not been implemented")
}

It worked for me. 它对我有用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 从初始化程序返回之前,并非在所有路径上都调用“ super.init” - 'super.init' isn't called on all paths before returning from initializer Swift:在从初始化程序返回之前没有在所有路径上调用“super.init”? - Swift: 'super.init' isn't called on all paths before returning from initializer? 在从初始化程序错误返回之前,不会在所有路径上调用Super.init - Super.init isn't called on all paths before returning from initializer Error 解决“从初始化快速返回之前未在所有路径上调用Super.init的问题”。 - Problems getting around 'Super.init isn't called on all paths before returning from initializer swift.' 从初始化程序返回之前不会调用Super.init - Super.init isn't called before returning from initializer 在从初始化程序返回之前,不会在所有路径上调用“self.init” - 'self.init' isn't called on all paths before returning from initializer json.swift 错误是 Self.init 未在委托初始化程序的所有路径上调用 - json.swift error is Self.init isn't called on all paths in delegating initializer RealmSwift初始值设定项:在super.init调用之前自行使用 - RealmSwift initializer: self used before super.init call 为什么不调用指定的初始化程序 - Why isn't the designated initializer being called swift3中无法调用super.init() - super.init() couldn't be called in swift3
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM