[英]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.