[英]swift initializer optional and non-optional at the same time?
https://github.com/Hearst-DD/ObjectMapper requires me to have https://github.com/Hearst-DD/ObjectMapper要求我拥有
required init?(_ map: Map){
}
I'd like to instantiate Foo()
but it is Foo? 我想实例化
Foo()
但是它是Foo吗? type not Foo. 输入非Foo。
I added the following initializer but no avail. 我添加了以下初始化程序,但无济于事。
override init() {
}
Take the Foo?, check if it has a value, and then unwrap it. 拿Foo ?,检查它是否有值,然后解开它。
let ff : Foo? = Foo()
if ff != nil {
let f : Foo = f!
// ...
}
Or more simply: 或更简单地说:
let ff = Foo()
if let f = ff {
// ...
}
** edit ** Sorry for the false call.. **编辑**对不起,打错电话了。
I had problem with the below code. 我的以下代码有问题。
var foo = Mapper<Foo>().map(jsonString)
if foo != nil {
return foo!
}
foo = Foo()
return foo
I think the problem is when i declared var foo
it's declared as Foo?
我认为问题是当我声明
var foo
时声明为Foo?
type and foo = Foo()
is still Foo?
类型和
foo = Foo()
仍然是Foo?
so I changed the code to 所以我将代码更改为
var foo = Mapper<Foo>().map(jsonString)
if foo != nil {
return foo!
}
return Foo()
and it seems working. 似乎可行。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.