Bird
Raised Fist0
Swiftprogramming~10 mins

Struct declaration syntax in Swift - Step-by-Step Execution

Choose your learning style10 modes available
Concept Flow - Struct declaration syntax
Start
↓
Write 'struct' keyword
↓
Name the struct
↓
Open curly brace '{'
↓
Declare properties and methods
↓
Close curly brace '}'
↓
Struct ready to use
This flow shows how to write a struct: start with 'struct', name it, add braces, then add properties or methods inside.
Execution Sample
Swift
struct Person {
    var name: String
    var age: Int
}
Defines a struct named Person with two properties: name and age.
Execution Table
StepCode PartActionResult
1struct PersonDeclare a struct named PersonStruct named Person created
2{Open struct bodyReady to add properties/methods
3var name: StringDeclare property 'name' of type StringProperty 'name' added
4var age: IntDeclare property 'age' of type IntProperty 'age' added
5}Close struct bodyStruct declaration complete
💡 Struct declaration ends after closing brace '}'
Variable Tracker
VariableStartAfter Step 3After Step 4Final
Person structNot declaredDeclared with 'name' propertyAdded 'age' propertyComplete with 'name' and 'age'
Key Moments - 2 Insights
Why do we use curly braces '{' and '}' in struct declaration?
Curly braces define the start and end of the struct body where properties and methods are declared, as shown in steps 2 and 5 of the execution table.
Can a struct have methods inside it?
Yes, methods can be added inside the braces just like properties, but this example only shows properties for simplicity.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution table, what happens at step 3?
AThe struct is named
BA property 'name' of type String is declared
CThe struct body is closed
DA method is added
💡 Hint
Check the 'Code Part' and 'Action' columns at step 3 in the execution table
At which step does the struct declaration complete?
AStep 5
BStep 2
CStep 3
DStep 4
💡 Hint
Look for the step where the closing brace '}' is processed in the execution table
If we add a method inside the struct, which step would it appear after?
AAfter step 1
BAfter step 5
CAfter step 2
DBefore step 1
💡 Hint
Methods go inside the braces, so after opening brace at step 2
Concept Snapshot
struct StructName {
  var propertyName: Type
  // methods can be added here
}
Use 'struct' keyword, name it, open braces, add properties/methods, close braces.
Full Transcript
This visual execution shows how to declare a struct in Swift. First, write the 'struct' keyword followed by the struct's name. Then open curly braces to start the struct body. Inside, declare properties like 'var name: String' and 'var age: Int'. Finally, close the braces to finish the struct declaration. The execution table traces each step, showing how the struct and its properties are created. Curly braces mark the struct's start and end. Methods can also be added inside the braces. This simple example helps beginners see how struct declaration works step-by-step.

Practice

(1/5)
1. What keyword is used to declare a struct in Swift?
easy
A. struct
B. class
C. function
D. var

Solution

  1. Step 1: Identify the keyword for struct declaration

    In Swift, the keyword struct is used to declare a struct type.
  2. Step 2: Differentiate from other keywords

    class declares classes, function is not a Swift keyword, and var declares variables.
  3. Final Answer:

    struct -> Option A
  4. Quick Check:

    Keyword for struct declaration = struct [OK]
Hint: Remember: struct keyword starts struct declarations [OK]
Common Mistakes:
  • Using class instead of struct
  • Confusing var with struct
  • Trying function keyword
  • Omitting the keyword
2. Which of the following is the correct syntax to declare a struct named Person with a property name of type String?
easy
A. struct Person = { var name: String }
B. struct Person (var name: String)
C. struct Person { var name: String }
D. struct Person: var name String

Solution

  1. Step 1: Check struct declaration syntax

    Structs use curly braces {} to enclose properties. Properties are declared with var or let inside braces.
  2. Step 2: Validate each option

    struct Person { var name: String } uses correct syntax: struct Person { var name: String }. Others misuse parentheses, equal sign, or colons incorrectly.
  3. Final Answer:

    struct Person { var name: String } -> Option C
  4. Quick Check:

    Struct syntax uses braces and var for properties [OK]
Hint: Use braces {} and var for properties inside struct [OK]
Common Mistakes:
  • Using parentheses instead of braces
  • Adding equal sign after struct name
  • Incorrect colon usage
  • Missing var keyword
3. What will be the output of this Swift code?
struct Car {
    var model: String
    func description() -> String {
        return "Car model is \(model)"
    }
}
let myCar = Car(model: "Tesla")
print(myCar.description())
medium
A. Car model is Tesla
B. Car model is model
C. Tesla
D. Error: Missing initializer

Solution

  1. Step 1: Understand struct with property and method

    The struct Car has a property model and a method description() that returns a string including the model.
  2. Step 2: Analyze code execution

    Creating myCar with model "Tesla" and calling description() returns "Car model is Tesla" which is printed.
  3. Final Answer:

    Car model is Tesla -> Option A
  4. Quick Check:

    Method returns string with model value [OK]
Hint: Methods can use properties with \(property) inside strings [OK]
Common Mistakes:
  • Expecting property name printed literally
  • Forgetting to call method
  • Assuming error due to no explicit initializer
  • Confusing output with just model string
4. Identify the error in this struct declaration:
struct Book {
    var title String
    var pages: Int
}
medium
A. Missing var keyword before pages
B. Curly braces missing
C. Struct keyword misspelled
D. Missing colon after title

Solution

  1. Step 1: Check property declarations

    Each property must have a colon between name and type, e.g., var title: String.
  2. Step 2: Identify the error

    The line var title String is missing the colon after title. Other parts are correct.
  3. Final Answer:

    Missing colon after title -> Option D
  4. Quick Check:

    Property syntax requires colon between name and type [OK]
Hint: Always put colon between property name and type [OK]
Common Mistakes:
  • Omitting colon between name and type
  • Forgetting var keyword
  • Misspelling struct
  • Missing braces
5. You want to create a struct Rectangle with properties width and height (both Int), and a method area() that returns the area as Int. Which is the correct complete declaration?
hard
A. struct Rectangle { var width Int var height Int func area() -> Int { return width + height } }
B. struct Rectangle { var width: Int var height: Int func area() -> Int { return width * height } }
C. struct Rectangle { var width: Int var height: Int func area() { width * height } }
D. struct Rectangle { width: Int height: Int func area() -> Int { return width * height } }

Solution

  1. Step 1: Check property declarations

    Properties must have var keyword and colon between name and type. struct Rectangle { var width: Int var height: Int func area() -> Int { return width * height } } correctly uses var width: Int and var height: Int.
  2. Step 2: Check method syntax and logic

    Method area() returns Int and correctly calculates width * height. struct Rectangle { var width: Int var height: Int func area() -> Int { return width * height } } matches this.
  3. Final Answer:

    Complete struct with correct properties and method -> Option B
  4. Quick Check:

    Properties need var and colon; method returns correct calculation [OK]
Hint: Use var with colon for properties; return correct calculation [OK]
Common Mistakes:
  • Omitting var keyword
  • Missing colon after property name
  • Method missing return type
  • Incorrect calculation in method
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard