如何从具有相同名称的嵌套类型的类中引用全局类型? [英] How to refer to a global type from within a class that has a nested type with the same name?
本文介绍了如何从具有相同名称的嵌套类型的类中引用全局类型?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个在全局范围内声明的类,另一个嵌套在某个类中的同名类.
I have a class declared at the global scope and another class with the same name that is nested within some class.
class Address {
var someProperty: String?
}
class ThirdPartyAPI {
class Address {
var someOtherProperty: String?
init(fromAddress address: Address) {
self.someOtherProperty = address.someProperty
}
}
}
问题是:如何从初始化程序中引用全局类而不是内部类?在给出的示例中,我出现了错误Value of type 'ThirdPartyAPI.Address' has no member 'someProperty'
,这意味着编译器引用内部的Address
而不是全局的.
The question is: how can I refer to a global class instead of the inner one from its initialiser? In the example given I've got an error Value of type 'ThirdPartyAPI.Address' has no member 'someProperty'
, which means that compiler refers to the inner Address
instead of a global one.
推荐答案
使用typealias
class Address {
var someProperty: String?
}
typealias GlobalAddress = Address
class ThirdPartyAPI {
class Address {
var someOtherProperty: String?
init(fromAddress address: GlobalAddress) {
self.someOtherProperty = address.someProperty
}
}
}
这篇关于如何从具有相同名称的嵌套类型的类中引用全局类型?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文