14

This is more of a coding style question but i believe it is valid. Coming from an obj c background i always create a .h and a .m when creating a class. However with swift that changes and all that goes into a single file. I know that for some people this is cool but i miss having these two things separate.

Quoting a comment from Zaph

"What I miss is a list of public methods as opposed to searching an entire source file for methods not marked private. There is a programming concept of "writing to the interface". And the public methods should be carefully picked, not just because the developer forgot to make some private."

is there a way to have a header - implementation class in separate files using swift? Maybe some trick?

Thanks

3
  • 1
    The closest I have tried is a protocol but there will be a naming issue, I just append "Interface" to the protocol name. Then make sure I mark all methods not in the protocol private. See this SO Answer.
    – zaph
    Aug 27, 2014 at 15:50
  • I believe you have said it just right. This is the closest one can get. Thanks. I will give it a try. Aug 27, 2014 at 15:50
  • +1 for protocols, check out this article to that effect: devblog.reverb.com/post/88673812266/…
    – Shizam
    Nov 18, 2014 at 2:40

5 Answers 5

25

May be you can use Generated Interface to view all the public methods and properties. You can find that option at the bottom of related files popup in the upper-left of the source editor window. To switch back, click Original Source in the same pop up.

Shortcut: control + cmd + down arrow

Generate Interface

This is how generated interface file looks.

Generated Interface file

0
3

As far as i'm aware, this cannot be done. That being said, if set out your .swift files correctly then they are still very readable. I tend to follow this as a guideline for styling in swift and i find that it breaks things up into readable sections, especially by using // MARK: as well.

0

In short, no.. But what do you miss..? Once you get used to it, you will probably prefer it like this! The old separation has no clear advantage over this new one!

3
  • 5
    What I miss is a list of public methods as opposed to searching an entire source file for methods not marked private. There is a programming concept of "writing to the interface". And the public methods should be carefully picked, not just because the developer forgot to make some private.
    – zaph
    Aug 27, 2014 at 15:26
  • 1
    All one needs to look at the the Interface, the rest is opaque to the user of the class. The class implementer is free to change, add and delete any method not exposed in the Interface without concern of effects elsewhere in the program. This reduces coupling.
    – zaph
    Aug 27, 2014 at 15:32
  • Yeah!! @Zaph you have said it just right! That is why. In fact i am going to edit my answer. Aug 27, 2014 at 15:40
0

More and more languages use this approach, as it reduce coupling and errors. So when you change the signature of a function, to need to check another file to update it, it's only duplication without any added value.

The problem you describe (how to see only "public" functions) is usually done buy tools( IDE) or documentation generators.

3
  • How does a single file "reduce coupling"? In Objective-C of late the only thing that needs to be in the Interface file is the public declarations.
    – zaph
    Aug 27, 2014 at 17:15
  • yes but then you need to manually copy informations from one file to the other as a developer.... For instance, swift don't require anymore to write an header file (but tis header file is automatically created by the tools if you want to use swift code in objective C)
    – tomsoft
    Aug 27, 2014 at 17:22
  • Yes, developers do need to perform work and creating a header file of the public interface it work. It is also documentation which I guess can also be skipped since it is work. The auto-created header file does no good for Swift classes using other Swift classes. What is lost is encapsulation (exposing only the public details) and writing to the interface.
    – zaph
    Aug 27, 2014 at 17:36
0

You can create 2 swift files:

  1. YourClassNameHeader.swift

class YourClassName {// put here all your properties
}

  1. YourClassNameMethods.swift or YourClassNamePrivate.swift

extension YourClassName { // put here all private methods and properties }

But in general its not good practise

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.