Welcome to the Treehouse Community
Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.
Looking to learn something new?
Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.
Start your free trialdeepak sharma
3,844 Pointswhat does static stand for in this video? please help me to understand
struct Point { let x: Int = 0 let y: Int = 0 }
struct Map { static let origin = Point()
static var x: Int {
return origin.x
}
}
1 Answer
krystennant
7,197 PointsIn this video, he is discussing type properties. These are properties that exist for the TYPE regardless of whether or not there are instances of the method. The definition of 'Static' means 'non moving'. Its not connected to any particular instance.
struct Map {
static let origin = Point(x: 0, y: 0)
// notice how there are no instances of Map created yet
Map.origin.x // returns 0
Map.origin.y // returns 0
This property exists even without instances and can be accessed using dot notation from the Type (ie Map.origin)
I have seen these used to denote counts and adding customized ID numbers before, see an example of how changing a type Property can be used:
struct Map {
static var origin = Point(x: 0, y: 0)
var instanceOrigin = origin
}
let firstMap = Map()
print(firstMap.instanceOrigin) // Point(x: 0, y: 0)
// changing the TYPE property
Map.origin = Point(x:1, y:1)
let secondMap = Map()
print(firstMap.instanceOrigin) // Point(x: 0, y: 0) stays the same
print(secondMap.instanceOrigin) // Point (x: 1, y: 1) inherits from the type property, which changed
The type property origin is associated with the Struct map - not the instances. Static just means it remains associated with the Type, not particular instances - it doesn't need instances to be accessed.
Hopefully this helps.