Without #import, does Swift have its own easy way to detect circular dependencies?

#import "whatever.h" 

... was not perfect, but it was very convenient for diagnosing circular dependencies, not to mention providing modularity.

You can be sure which classes you knew about other classes - with the click of a finger.

If you need to, you can comment on all import statements and add them one at a time to diagnose the dependency problem. It was not necessarily fast, but it was simple.

And if the class did not import anything but the required headers, the Son, that one modular class that you had there!

If there were ten classes in your project that didn’t import anything, then you knew that these were ten modular classes — you did not need to pack each class into its own Framework or something like that. Easy.

But now that Swift's “everyone knows everything all the time” policy, it looks like it's just personal vigilance to maintain modularity. Personal vigilance is the worst kind!

Am I missing something? Is there a way to easily do this in Swift?

+7
swift
source share
1 answer

If you want to modulate your Swift code, you must use modules!

Creating a new module is quite simple.

Add a new goal to your project by clicking the plus sign here:

enter image description here

Select "Framework and Library" for the appropriate platform (iOS / OS X):

enter image description here

Select "Cocoa Framework" (or Cocoa Touch, platform dependent) and click "Next":

enter image description here

Give your module a name and change the language to Swift (or leave it as Objective-C, it does not matter, you can use both).

enter image description here

Add to your Swift file:

enter image description here

Add the code to your Swift file. Please note: the default access level for Swift is internal , which means that it can be accessed from anywhere in the module, but not from outside the module. Any code that we want to use outside the module must have a public access level.

 public class ModularSwift { public init(){} public var foo: Int = 0 } 

Be sure to create your module (Cmd + B):

enter image description here

Return to the original goal, import the module and start using its code:

 import MyModularSwiftCode let foo = ModularSwift() 

Xcode is completely happy:

enter image description here

Now comment out the import statement and notice the errors:

enter image description here

+6
source share

All Articles