0
0
Swiftprogramming~5 mins

Class declaration syntax in Swift - Cheat Sheet & Quick Revision

Choose your learning style9 modes available
Recall & Review
beginner
What is the basic syntax to declare a class in Swift?
Use the <code>class</code> keyword followed by the class name and curly braces. Example:<br><pre>class MyClass {
    // properties and methods
}</pre>
Click to reveal answer
beginner
How do you define a property inside a Swift class?
Inside the class braces, declare a variable or constant using <code>var</code> or <code>let</code>. Example:<br><pre>class Person {
    var name: String
}</pre>
Click to reveal answer
intermediate
How do you create an initializer in a Swift class?
Use the <code>init</code> method inside the class to set up initial values. Example:<br><pre>class Person {
    var name: String
    init(name: String) {
        self.name = name
    }
}</pre>
Click to reveal answer
intermediate
What keyword is used to inherit from another class in Swift?
Use a colon <code>:</code> after the class name followed by the parent class name. Example:<br><pre>class Student: Person {
    var grade: Int
}</pre>
Click to reveal answer
beginner
Can Swift classes have methods? How do you declare one?
Yes, methods are functions inside a class. Declare them with <code>func</code>. Example:<br><pre>class Person {
    func greet() {
        print("Hello!")
    }
}</pre>
Click to reveal answer
Which keyword starts a class declaration in Swift?
Aclass
Bstruct
Cfunc
Dvar
How do you indicate that a class inherits from another class?
AUsing a colon <code>:</code> after the class name
BUsing the <code>extends</code> keyword
CUsing the <code>inherits</code> keyword
DUsing parentheses <code>()</code> after the class name
What is the purpose of the init method in a Swift class?
ATo declare a property
BTo initialize the class with starting values
CTo create a new method
DTo inherit from another class
Which keyword do you use to define a method inside a Swift class?
Amethod
Bprocedure
Cdef
Dfunc
How do you declare a variable property inside a Swift class?
ApropertyName: Type
Blet propertyName: Type
Cvar propertyName: Type
Ddef propertyName: Type
Explain how to declare a simple class in Swift with one property and an initializer.
Think about how you create a blueprint for an object with starting values.
You got /4 concepts.
    Describe how inheritance works in Swift class declarations and how to write it in code.
    Inheritance means one class gets features from another.
    You got /4 concepts.