101.school
CoursesAbout
Search...⌘K
Generate a course with AI...

    Learn Swift Step By Step

    Receive aemail containing the next unit.
    • Introduction to Swift
      • 1.1Swift Language Overview
      • 1.2Basic Syntax in Swift
      • 1.3Data Types and Variables
      • 1.4Operators and Control Flow in Swift
    • Classes,Properties and Methods in Swift
      • 2.1Introduction to Classes and Objects
      • 2.2Properties
      • 2.3Methods
      • 2.4Inheritance, Polymorphism & Protocols
    • Collection Types in Swift
      • 3.1Introduction to Arrays, Sets and Dictionaries
      • 3.2Basic Collection Operations
      • 3.3Looping Over Collections & Modifying Collections
      • 3.4Advanced Collection Types
    • Advanced Swift
      • 4.1Error Handling in Swift
      • 4.2Extensions and Protocols
      • 4.3Generics
      • 4.4Concurrency and Multi-Threading in Swift

    Classes,Properties and Methods in Swift

    Understanding Methods in Swift

    Methods are self-contained chunks of code that perform a specific task. In Swift, methods are associated with a particular type, and they can be either instance methods (associated with an instance of a type) or type methods (associated with the type itself).

    Instance Methods

    Instance methods are functions that belong to instances of a particular class, structure, or enumeration. They have implicit access to all other instance methods and properties of that type.

    Here's an example of an instance method in Swift:

    class Counter { var count = 0 func increment() { count += 1 } } let counter = Counter() // Creating an instance of Counter counter.increment() // Calling the instance method print(counter.count) // Prints "1"

    In this example, increment() is an instance method. We first create an instance of Counter (i.e., counter), and then call the increment() method on this instance.

    Type Methods

    Type methods are similar to class methods in other languages. You indicate type methods by writing the keyword static before the method’s func keyword. Classes may also use the class keyword to allow subclasses to override the superclass’s implementation of that method.

    Here's an example of a type method in Swift:

    class SomeClass { static func someTypeMethod() { // type method implementation goes here } } SomeClass.someTypeMethod() // Calling the type method

    In this example, someTypeMethod() is a type method. We call it on the class itself, not on an instance of the class.

    Method Parameters and Return Types

    Swift methods can have parameters and return values, just like functions. Here's an example:

    class Rectangle { var width: Int var height: Int init(width: Int, height: Int) { self.width = width self.height = height } func area() -> Int { return width * height } } let rectangle = Rectangle(width: 5, height: 10) print(rectangle.area()) // Prints "50"

    In this example, the area() method takes no parameters but returns an Int value.

    Overloading Methods

    Method overloading allows a class to have two or more methods having the same name, if their parameter lists are different. Here's an example:

    class Display { func printValue(_ value: Int) { print("Integer: \(value)") } func printValue(_ value: String) { print("String: \(value)") } } let display = Display() display.printValue(5) // Prints "Integer: 5" display.printValue("Hello") // Prints "String: Hello"

    In this example, we have two printValue() methods: one that takes an Int parameter, and one that takes a String parameter.

    By understanding how to use methods in Swift, you can create more organized and modular code.

    Test me
    Practical exercise
    Further reading

    Howdy, any questions I can help with?

    Sign in to chat
    Next up: Inheritance, Polymorphism & Protocols