Class vs. Instance Properties & Methods
Master Swift class design and object-oriented programming fundamentals
Core Concepts You'll Master
Class vs Instance Properties
Learn the fundamental difference between static class properties that belong to the type itself and instance properties that vary between objects.
Methods and Enumerations
Discover how to integrate enums with classes and create both instance and class methods for different use cases.
Object-Oriented Design
Build practical skills with real-world examples using a Puppy class that demonstrates key programming principles.
Tutorial Learning Path
Enum Integration
Start with basic enumerations and learn to incorporate them within class structures
Instance Methods
Create and use methods that operate on specific instances of your classes
Class Properties
Understand static properties that belong to the class itself, not individual instances
Advanced Methods
Build class methods that can generate new instances and perform class-level operations
Playground Setup Requirements
Use File > New > Playground and select Blank template under iOS
Navigate to Desktop > Class Files > yourname-iOS App Dev 1 Class
Save as 'Class Properties and Methods.playground' for organization
Delete only the var str line, keep import UIKit for later random functions
When associating String raw values with enum cases, specify the enum type as String and ensure all cases use the same data type. This enables accessing values with .rawValue property.
Enum Usage: Standalone vs Class Integration
| Feature | Standalone Enum | Enum in Class |
|---|---|---|
| Declaration | var gender: Gender = .male | var gender: Gender // as property |
| Access Method | Direct variable access | Through class instance |
| Initialization | Immediate assignment | Via class initializer |
| Scope | Global or local | Encapsulated in class |
Instance methods perform an action on an instance of a type, accessing and using the specific data of that particular object.
Complex print statements appear in the Debug area at the bottom of Xcode, while simple values show in the right sidebar. Use print() for detailed formatted output.
Property Types Comparison
| Feature | Instance Properties | Class Properties |
|---|---|---|
| Declaration | var name: String | static var puppies = [Puppy]() |
| Access Pattern | puppy1.name | Puppy.puppies |
| Uniqueness | Different per instance | Single copy for entire class |
| Use Cases | Individual object data | Shared class-wide data |
Class properties cannot be accessed through instances. Use ClassName.property, not instanceName.property, or you'll get a compilation error.
Class Method Capabilities
Instance Generation
Create and return new instances of the class with methods like spawnRandomPuppy() that generate objects with random or computed properties.
Collection Operations
Operate on class-wide data like the puppies array, performing actions across all instances such as listing all puppies.
Utility Functions
Provide class-level functionality that doesn't require a specific instance, like random generation or bulk operations.
Key Takeaways
.