How to use Namespaces in Swift?

后端 未结 8 1982
Happy的楠姐
Happy的楠姐 2020-11-28 01:53

The documentation only mentions nested types, but it\'s not clear if they can be used as namespaces. I haven\'t found any explicit mentioning of namespaces.

8条回答
  •  天涯浪人
    2020-11-28 02:32

    Answered by SevenTenEleven in the Apple dev forum:

    Namespaces are not per-file; they're per-target (based on the "Product Module Name" build setting). So you'd end up with something like this:

    import FrameworkA
    import FrameworkB
    
    FrameworkA.foo()
    

    All Swift declarations are considered to be part of some module, so even when you say "NSLog" (yes, it still exists) you're getting what Swift thinks of as "Foundation.NSLog".

    Also Chris Lattner tweeted about namespacing.

    Namespacing is implicit in Swift, all classes (etc) are implicitly scoped by the module (Xcode target) they are in. no class prefixes needed

    Seems to be very different what I have been thinking.

提交回复
热议问题