相关疑难解决方法(0)

在Swift中返回instancetype

我正在尝试进行此扩展:

extension UIViewController
{
    class func initialize(storyboardName: String, storyboardId: String) -> Self
    {
        let storyboad = UIStoryboard(name: storyboardName, bundle: nil)
        let controller = storyboad.instantiateViewControllerWithIdentifier(storyboardId) as! Self

        return controller
    }
}
Run Code Online (Sandbox Code Playgroud)

但我得到编译错误:

错误:无法将'UIViewController'类型的返回表达式转换为返回类型'Self'

可能吗?我也想成为init(storyboardName: String, storyboardId: String)

swift swift-extensions swift2

35
推荐指数
2
解决办法
1万
查看次数

Swift协议定义返回self的类方法

我的代码在XCode 6 beta中运行,但在更新到xcode 6.1之后最近停止了工作.

这是我的协议:

protocol CanDeserialiseFromJson {
    class func FromJson(json : JSONValue) -> Self
}
Run Code Online (Sandbox Code Playgroud)

这是实施:

extension Invoice : CanDeserialiseFromJson {
    class func FromJson(json : JSONValue) -> Self {
        return Invoice()
    }
}
Run Code Online (Sandbox Code Playgroud)

这没有给出错误:

'Invoice' is not convertable to 'Self'
Run Code Online (Sandbox Code Playgroud)

正如我所说,这曾经工作,我无法解决为什么它不再存在

protocols swift

6
推荐指数
1
解决办法
3070
查看次数

标签 统计

swift ×2

protocols ×1

swift-extensions ×1

swift2 ×1