Classes, Properties, & Methods
Master Swift Classes and Object-Oriented Programming Fundamentals
Core iOS Development Concepts
Object-Oriented Programming
Learn the foundational programming paradigm that makes Swift powerful. Objects communicate through messages rather than linear execution.
Classes & Methods
Understand how to create blueprints for objects with custom functionality. Classes serve as templates for building app components.
Properties & Parameters
Master data storage within classes and method communication. Properties hold object characteristics while parameters enable dynamic behavior.
Traditional vs Object-Oriented Programming
| Feature | Traditional Programming | Object-Oriented Programming |
|---|---|---|
| Code Execution | Linear, line-by-line | Message-based between objects |
| Code Reusability | Limited reusability | High reusability through classes |
| Maintenance | Becomes nightmare with scale | Organized, manageable structure |
| Code Connections | Difficult to establish | Meaningful object relationships |
Creating Your First Swift Class
Define the Class
Use the 'class' keyword followed by a noun name. Everything goes within curly braces that follow the class declaration.
Add Custom Methods
Create functions within the class using 'func' keyword. These become methods associated with the class type.
Implement Functionality
Add print statements or other logic within method bodies to define what each method accomplishes.
Classes are usually given noun names. Think of a class like a car frame used for various models - the same template creates different objects with unique characteristics.
Objects are instances of classes so the process of bringing an object to life is called instantiation.
Object Instantiation Process
Create Instance
Declare a constant or variable and set it equal to the class name followed by parentheses.
Access Methods
Use dot syntax with the instance name to call methods: instanceName.methodName()
Verify Results
Check the results sidebar in Xcode to see method outputs and confirm proper execution.
When working within a class, the instance of that class is called 'self'. Use this keyword to set property values or call methods from within the class itself.
External vs Internal Property Access
| Feature | External Access | Internal Access |
|---|---|---|
| Syntax | instanceName.property | self.property |
| Location | Outside the class | Inside class methods |
| Efficiency | Manual setting required | Automatic within methods |
Methods are functions that exist within a class. Like functions, methods can accept parameters and return data, but they operate on class instances.
Creating Parameterized Methods
Define Method Signature
Include parameter name and type within parentheses after the method name.
Implement Method Body
Use the parameter value within the method to modify properties or perform calculations.
Call with Arguments
Provide specific values when calling the method using the parameter label syntax.
Implementing Return Methods
Specify Return Type
Add arrow syntax and return type after method parameters to indicate what data type the method returns.
Calculate and Store
Create constants or variables within the method to perform calculations using class properties.
Return Result
Use the 'return' keyword to send the calculated value back to the method caller.
The mpg property can be set differently for each car instance. This demonstrates how object-oriented programming enables reusable code with customizable characteristics.
Key Takeaways

. The popup shows {some 50}, confirming the speed was updated.