0

I have just started to shift from Obj-C to Swift in Xcode.

My question is about the new .swift files which replace the .h and .m files. The answers to this question partially tells me the differences, but I am still puzzled by the issue of 'privacy' with the new .swift file.

If I look up the definition of a class in Obj-C, I will be only be able to view the .h file (@interface) which contains the declaration of the class, its variables and methods. I cannot view the .m file (@implementation) which contains the code which controls how the class works (they are private which stops others from seeing/possibly plagiarising your code, I guess).

However, in Swift, it seems that something such as:

class NewClass: ParentClass {...}

creates a class in one go without the need for 2 sections - the @interface and @implementation. But when I look up the definition of a class (say, SKScene) in Swift, the code for the methods, etc, are still kept private somehow... How is this done?

Community
  • 1
  • 1
Shuri2060
  • 729
  • 6
  • 21

1 Answers1

2

The same situation in which you only see header files (e.g. a pre-compiled framework) will, with Swift, only make the interface available. The Swift files themselves will not be included in the framework. Instead, Xcode will generate pseudo-interface files that will be visible to users of the framework. You get property definitions and method signatures, but no implementations.

Avi
  • 7,469
  • 2
  • 21
  • 22
  • So this is all done for me automatically without me needing to bother creating anything extra? – Shuri2060 Jul 10 '16 at 11:09
  • 1
    That's my understanding; and for a preview, click the four squares in the top left corner of an editing view containing a Swift file and select 'Generated Interface'. That's what will be published. Switch back to 'Original Source' to perform editing. – Tommy Jul 10 '16 at 11:14
  • What about the comments you typically see in the foundation classes, etc. (eg. definition for SKScene). Would you have to add them in directly? If so, wouldn't this clutter your code? (In Obj-C it wouldn't as the comments were added in the .h file) – Shuri2060 Jul 10 '16 at 11:24
  • 1
    They have to be inline. If you come from Java, this is totally normal, though you generally don't view documentation that way in Java. – Avi Jul 10 '16 at 11:33
  • 2
    It's far from abnormal in Xcode-hosted languages nowadays. Inline documentation, via /*! or /** or by other means is automatically parsed and is available pervasively as quick help. E.g. add `/*! This method is cool. */` above any method definition. Possibly give Xcode sufficient time to index. Go to any usage of the method. Press option and click. Your documentation should pop up as the help bubble. In C, C++ and Objective-C the normal Java headerdoc keywords — `@param`, `@returns`, etc — should all work; but in Swift you can instead use `- param` and more, which then permits normal Mardown. – Tommy Jul 10 '16 at 11:49