将目标c更改为快速代码方式

时间:2018-02-08 06:34:08

标签: swift

如何在swift 3中声明:

// objective c
static id currentInstance;

然后执行此操作

// objective c
+ (id)getCurrentViewController {
    return currentInstance;
}

2 个答案:

答案 0 :(得分:2)

static let currenInstance: Any = /*YourClass*/()

static func getCurentViewController() -> Any {
    return /*YourClass*/.currentInstance
}

答案 1 :(得分:1)

这样的事情:

class CurrentInstanceHolder {
    private static var currentInstance:Any!

    static func getCurrentInstance() -> Any {
        return currentInstance  
    }
    static func setCurrentInstance(_ instance:Any) {
        currentInstance = instance;
    }
}

CurrentInstanceHolder.setCurrentInstance("aa")

print(CurrentInstanceHolder.getCurrentInstance())

您可以使用Any或您的特定类型,而不是AnyObject。您也可以将所有静态信息放入具体类型中。