简体   繁体   中英

How can a non-instantiated object be accessed via dot notation? (Swift Playground)

I'm confused about the coordinate.column and coordinate.row used in the code swift plyground provided below. How was column and row accssed when I didn't instantiate the instance coordinate? If the for loop insatntiate coordinate, how was it instantiated when allcoordinates or world.allPossibleCoordinates are not a type? (There are no parathenthesis around world.allPossibleCoordinates...)

let allCoordinates = world.allPossibleCoordinates
var blockSet: [Coordinate] = []

//#-editable-code Tap to enter code
for coordinate in allCoordinates {
    // Check for coordinates with a column > 5 OR a row < 4.
    if coordinate.column > 2 && coordinate.row < 5 {
        // Append coordinate to blockSet.

    }
}

In Swift , the for in loop works a bit differently than a for loop in other languages. A for in loop strides over a range (I'm assuming allCoordinates is strideable). In the provided example, if the variable allCoordinates is a strideable range, the loop will go through every single item in that range assigning each value to coordinate per iteration. For more information, have a look at Apple's Documentation

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM