Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

iOS Intermediate Swift 2 Value Semantics, Type Methods and Inheritance Type Methods

Paul M
Paul M
16,370 Points

What does "static" mean in Swift?

In the code he worked on, he added static before a constant and a function. But what does it really do, and what are the benefits and downsides of using static?

2 Answers

cm21
cm21
150,854 Points

Hi Paul,

The static keyword is used to indicate that a method is a type method. Type methods contrast with instance methods, in that:

  • Instance methods are called on an individual instance of one unique/chosen type.
  • Type methods are called on the actual type, in and of itself.

Additional notes:

  • Subclasses can override class methods; however, they cannot override static methods.
  • Concerning constants: similarly, static constants should not be overridden, as opposed to non-static constants.

A page that may prove useful: http://sketchytech.blogspot.com/2014/09/swift-rules-of-being-static-xcode-6-gm.html.

Hope this helps!

thanks a lot