Swift: Class Prefix Needed?

前端 未结 2 592
梦谈多话
梦谈多话 2020-12-01 04:33

Should I give my Swift class names a three-letter prefix as recommended by Objective-C Conventions: Class Names Must Be Unique Across an Entire App?

2条回答
  •  情歌与酒
    2020-12-01 04:43

    No, prefix is definitely not neeeded.

    Suppose your app has MyApp name, and you need to declare your custom UICollectionViewController.

    You don't need to prefix and subclass like this:

    class MAUICollectionViewController: UICollectionViewController {}
    

    Do it like this:

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

    Why?. Because what you've declared is declared in current module, which is your current target. And UICollectionViewController from UIKit is declared in UIKit module.

    How to use it within current module?

    var customController = UICollectionViewController() //your custom class
    var uikitController = UIKit.UICollectionViewController() //class from UIKit
    

    How to distinguish them from another module?

    var customController = MyApp.UICollectionViewController() //your custom class
    var uikitController = UIKit.UICollectionViewController() //class from UIKit
    

提交回复
热议问题