Nat*_*Mio 12 objective-c ios swift
我曾经在我的类NSObject构造函数中使用以下代码使用Objective-c:
- (id)init {
self = [super init] ;
return self;
}
Run Code Online (Sandbox Code Playgroud)
我如何在Swift中使用它?
我试着这样做:
override init() {
self = super.init()
return self;
}
Run Code Online (Sandbox Code Playgroud)
我有两个错误:
cannot assign value self is immutable
nil is the only return value permitted in this initializer
Run Code Online (Sandbox Code Playgroud)
Avi*_*Avi 17
你无法self在Swift中分配.只是用
super.init()
Run Code Online (Sandbox Code Playgroud)
你也没有退货.构造函数类型为void(在C lingo中).
小智 8
Swift初始化序列与Objective-C略有不同,
class BaseClass {
var value : String
init () {
value = "hello"
}
}
Run Code Online (Sandbox Code Playgroud)
下面的子类.
class SubClass : BaseClass {
var subVar : String
let subInt : Int
override init() {
subVar = "world"
subInt = 2015
super.init()
value = "hello world 2015" // optional, change the value of superclass
}
}
Run Code Online (Sandbox Code Playgroud)
初始化序列是:
我认为你的代码:
override init() {
self = super.init() //just call super.init(), do not assign it to self
return self; //it is unnecessary to return self
}
Run Code Online (Sandbox Code Playgroud)
我们必须记住初始化类中的所有var或let.
| 归档时间: |
|
| 查看次数: |
15270 次 |
| 最近记录: |