Swift:需要类前缀吗?

时间:2014-06-13 23:00:01

标签: objective-c naming-conventions swift

我应该按照Objective-C Conventions: Class Names Must Be Unique Across an Entire App的推荐给我的Swift类名称设置三个字母的前缀吗?

2 个答案:

答案 0 :(得分:68)

不,you do not need class prefixes in Swift,因为类被命名为它们所在的模块。

如果您需要消除(例如)Swift中的Array和您在应用中声明的Array类/结构之间的歧义,可以将其键入为一个Swift.ArrayMyProject.Array。这也适用于扩展:

extension Swift.Array {
    ...
}

extension MyProject.Array {
    ...
}

答案 1 :(得分:16)

不,绝对不需要前缀。

假设您的应用具有MyApp名称,则需要声明自定义UICollectionViewController

不需要来加上前缀和子类,如下所示:

class MAUICollectionViewController: UICollectionViewController {}

这样做:

class UICollectionViewController {} //no error "invalid redeclaration o..."

<强>为什么吗。因为您声明的内容是在当前模块中声明的,这是您的当前目标。来自UICollectionViewController的{​​{1}}在UIKit模块中声明。

如何在当前模块中使用它?

UIKit

如何区别于其他模块?

var customController = UICollectionViewController() //your custom class
var uikitController = UIKit.UICollectionViewController() //class from UIKit
相关问题